Home > Net >  LiteralPath does not work. What am I doing wrong?
LiteralPath does not work. What am I doing wrong?

Time:10-09

I am a beginner and do not know where the error is in my PowerShell script. After a long Google search and a lot of trial and error, I haven't found a solution.

Without LiteralPath the script works. However, I need LiteralPath so that I can use German letters like "ä" in paths.

$d = Get-ChildItem -LiteralPath "G:\ä\*.jpg" | resolve-path -LiteralPath | get-random -count 200
Move-Item -LiteralPath $d -destination G:\ä\ä

CodePudding user response:

G:\ä\*.jpg is a wildcard pattern, so by definition it won't work with -LiteralPath, which uses its arguments literally (verbatim).

Non-ASCII-range characters such as ä should always work in file paths in PowerShell (with PowerShell-native commands), whether or not you use -Path with a wildcard pattern or -LiteralPath with a literal path.

The following streamlined version of your command should work; if it doesn't, the problem is most likely unrelated to non-ASCII-range characters in the paths:

Get-ChildItem -LiteralPath G:\ä -Filter *.jpg |
  Get-Random -Count 200 |
    Move-Item -Destination G:\ä\ä -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

  • Related