# Try to replace german special chars in filenames

**URL:** <https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263>\
**Category:** PowerShell Help\
**Created:** [December 6, 2023, 10:01pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263 "2023-12-06T22:01:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![widdi](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@widdi](https://forums.powershell.org/u/widdi)\
**Post date:** [December 6, 2023, 10:01pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/1 "2023-12-06T22:01:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 6, 2023, 10:22pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/2 "2023-12-06T22:22:44Z")

</div>

Frank,  
Welcome to the forum. 👋🏼

I’d do it this way:

```auto
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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

---

<div class="post-metadata">

**Author:** ![widdi](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@widdi](https://forums.powershell.org/u/widdi)\
**Post date:** [December 6, 2023, 10:44pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/3 "2023-12-06T22:44:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 6, 2023, 11:57pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/4 "2023-12-06T23:57:23Z")

</div>

> [@widdi](#):
>
> If using a link (open normally and open as administrator) it will only replace the blank, but not the german chars äöüß.

I can actually reproduce what you’re describing. It’s weird. 🤨 But I actually don’t have a solution. It seems to be a bug in Windows PowerShell. 🤷🏼‍♂️

You could circumvent the issue by installing PowerShell 7.x. I tested it and it just works as expected with PowerShell 7.4. 👍🏼 🤟🏼

> [@widdi](#):
>
> I think it is the problem with **-ExecutionPolicy Bypass** in my link.

No. 😏

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 7, 2023, 8:44am UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/5 "2023-12-07T08:44:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 7, 2023, 9:11am UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/6 "2023-12-07T09:11:16Z")

</div>

… just tested it and it works. 👍🏼 🤟🏼 … strange, but cool strange. 🤷🏼‍♂️

Thanks 🙏🏼

---

<div class="post-metadata">

**Author:** ![widdi](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@widdi](https://forums.powershell.org/u/widdi)\
**Post date:** [December 7, 2023, 6:32pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/7 "2023-12-07T18:32:28Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:13pm UTC](https://forums.powershell.org/t/try-to-replace-german-special-chars-in-filenames/23263/8 "2024-05-16T20:13:38Z")

</div>

Closed on May 16, 2024, 8:13pm UTC


