Gunni
Gunni

Reputation: 519

German umlauts in powershell. Iterate over directory does not work

I am trying to iterate over a folder. It works for all folder but not for directories with umauts in it. Following script shows what the problem is:

$source= @("D:\XLS_Abrufe-ÜNB")
foreach ($element in $source) {
    $element
    cmd /c dir /ad /b /s $element |foreach{
        $_
    }
}

Output:

D:\XLS_Abrufe-ÜNB

D:\XLS_Abrufe-šNB\2015
D:\XLS_Abrufe-šNB\2016
D:\XLS_Abrufe-šNB\2017

If i try to work on with these names, it won't find the folders. Any ideas how to get this with work with umlauts?

Upvotes: 3

Views: 661

Answers (2)

briantist
briantist

Reputation: 47872

You are shelling out an external command, in this case the cmd prompt, which uses the console host. In Windows, the console host still uses code pages, so while PowerShell understands the character just fine, the console host is likely not understanding the encoding.

If you use native PowerShell all the way (as in TheIncorrigible1's answer) it should work just fine.

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19694

Are you trying to get all of the directories within D:\XLS_Abrufe-ÜNB? From my experience, cmd does not handle unicode very well.

$source= @("D:\XLS_Abrufe-ÜNB")
Get-ChildItem $source -force |
  Where-Object { $_.IsPSContainer } |
  $_

Upvotes: 2

Related Questions