Try to replace german special chars in filenames

Hello community,

I’m beginner with ps and googling around for my problem. We’re using “imperia” for our websites. This tool has sometimes problems with special characters. So I will rename the files.

I wrote that script

$files = Get-Childitem
foreach ($file in $files)
{
$newFileName=$file.Name.ToLower()
$newFileName=$newFileName.Replace(“ä”,“ae”)
$newFileName=$newFileName.Replace(“ö”,“oe”)
$newFileName=$newFileName.Replace(“ü”,“ue”)
$newFileName=$newFileName.Replace(“ß”,“ss”)
$newFileName=$newFileName.Replace(" “,”_")
Rename-Item $file $newFileName
}

If using it inside the normal powershell-commandline - works.

Now I saved it as a filename and created a link in the directory, I want to rename the files (in my case “execute in dir”: c:\temp).

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File “datei_umbennen.ps1”

when opening the link, then die Files will be editet. but… only these two lines for manipulating the filename will be executed.

$newFileName=$file.Name.ToLower()
$newFileName=$newFileName.Replace(" “,”_")

the other 4 lines with the german special chars will be ignored
can you imagine, why these lines will not be affected? thanks for a little help.

Frank,
Welcome to the forum. :wave:t3:

I’d do it this way:

Get-Childitem |
    ForEach-Object {
        $newFileName = ($_.BaseName -creplace 'Ü','Ue' -creplace 'Ö','Oe' -creplace 'Ä','Ae' -creplace 'ü','ue' -creplace 'ö','oe' -creplace 'ä','ae' -replace 'ß','ss' -replace '\s+','_') + $_.Extension
        Rename-Item -Path $_.FullName -NewName $newFileName
    }

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Hello Olaf,

thank you for your answer. your code works very well. but I have the same problem:

If using a link (open normally and open as administrator) it will only replace the blank, but not the german chars äöüß.

If opening the normal ps console and insert the script - all letters will be edited

I think it is the problem with -ExecutionPolicy Bypass in my link.

ah you’re right - the preformatted text button was behind the gear symbol. So I only saw “quote”. thanks for the hint

I can actually reproduce what you’re describing. It’s weird. :face_with_raised_eyebrow: But I actually don’t have a solution. It seems to be a bug in Windows PowerShell. :man_shrugging:t3:

You could circumvent the issue by installing PowerShell 7.x. I tested it and it just works as expected with PowerShell 7.4. :+1:t3: :love_you_gesture:t3:

No. :smirk:

I have seen this before in a similar scenario (replacing the characters in usernames).

Try changing the encoding of the script file to UTF-8 with BOM rather than UTF-8. In VS Code the encoding is in the bottom right of the window or you can open the file in Notepad and choose the encoding in the Save As dialog.

1 Like

… just tested it and it works. :+1:t3: :love_you_gesture:t3: … strange, but cool strange. :man_shrugging:t3:

Thanks :pray:t3:

1 Like

thank you all for supporting. it works. the idea of changing the coding of the file… unbelievable. I try to find out the “intern mapping”. I know such errors when working with textfiles and visual studio :slight_smile: