Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell bin cue rename
# 2022-05-31
PS> Get-ExecutionPolicy # PowerShell 5.1 (default in Win10)
PS> Set-ExecutionPolicy Unrestricted -Force # as admin
PS> Get-ExecutionPolicy # PowerShell 7.2.3 (installed in Win10)
In PowerShell 6+, the default encoding 'utf8NoBOM' is UTF-8 without BOM on all platforms.
But option 'utf8NoBOM' is not available in previous PowerShell 5.1 (in Win10 default).
In Windows PowerShell 5, the default encoding 'Default' (usually ANSI) is usually Windows-1252,
an extension of latin-1, also known as ISO 8859-1. Options 'UTF8' in PowerShell 5.1 is with BOM.
!!! Change $OutputEncoding to use UTF-8 NoBOM encoding rather than ASCII !!!
The previous encoding, ASCII (7-bit), would result in incorrect alteration of the output in some cases.
Making UTF-8 NoBOM the default preserves Unicode output with an encoding supported by most tools
and operating systems.
'utf8NoBOM' UTF-8 without BOM is best choice and is default in PowerShell 6+.
'-Filter' is faster, and the results are more predictable.
'-Filter' accepts only a single string. '-Include' accepts multiple values.
Get-ChildItem -File -Recurse -Filter "*.ogg"
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.ogg", "*.mp3" | ForEach-Object {
$BinName = $_.Name
$BinFullName = $_.FullName
Write-Host("bin file :", $BinFullName)
$CueFullName = [System.IO.Path]::ChangeExtension($BinFullName, 'cue')
$CueTest = [System.IO.Path]::ChangeExtension($BinFullName, 'test.cue') # test
Write-Host("cue file :", $CueFullName)
$content = Get-Content -Path $CueFullName
$lineOld = $content | Select-String 'FILE "' | Select-Object -ExpandProperty Line
Write-Host("line old :", $lineOld)
$lineNew = 'FILE "' + $BinName + '" WAVE'
Write-Host("line new :", $lineNew)
# $FinalCueName = $CueFullName # rewrite original CUE file
$FinalCueName = $CueTest # create new test CUE file
# $content -replace $lineOld, $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # problem with parentheses in PowerShell
# $content -replace [Regex]::Escape($lineOld), $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # OK
# don't require PowerShell 6+, this method take care of the encoding of the file (UTF-8 without BOM)
$content2 = [System.IO.File]::ReadAllText($CueFullName).Replace($lineOld, $lineNew)
[System.IO.File]::WriteAllText($FinalCueName, $content2)
# 2022-05-12
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.srt" | ForEach-Object {
$FileFullName = $_.FullName
Write-Host("file :", $FileFullName)
$FileTest = [io.path]::ChangeExtension($FileFullName, '') # test
$content = Get-Content -Path $FileFullName
# $content | Set-Content -Encoding utf8NoBOM -Path $FileFullName -Force # rewrite original SRT file
$content | Set-Content -Encoding utf8NoBOM -Path $FileTest -Force # create new test SRT file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment