How to rename a directory with international characters in PowerShell?

I have a PowerShell script that among other things renames a directory on the filesystem. Some of the directories that I am attempting to rename have non-english characters in their names, and after the script is finished executing these folders names are garbled. I have written an example script to illustrate the problem.

$NewDir = New-Item -Path "c:\" -Name "New Directory" -ItemType "directory"

Write-Host "Created Directory - " $NewDir.FullName

$RenamedDir = Rename-Item -Path "c:\New Directory" -NewName "Yeni Direktör" -PassThru

Write-Host "Renamed Directory - " $RenamedDir.FullName

I am launching this script via a windows batch file with the following command:

powershell.exe -executionpolicy remotesigned -file "PowerShellInternationalRenameExample.ps1"

When the script finishes execution, instead of the newly renamed directory having the name "Yeni Direktör" (which is Turkish for "New Directory") the resulting directory is named "Yeni Direktör". How do I rename the directory so it is not garbled?

EDIT- According to Notepad++ the script file itself is encoded in UTF-8


Solution 1:

PowerShell does not guess correctly the encoding of UTF-8 files without the Byte order mark (BOM).

For example, passing the script through Notepad++ and specifying in menu Encoding "Encode in UTF-8-BOM" should fix the file. Better to check if the BOM bytes were truly added at the beginning of the file.