运行 带有可变 -Path 参数的 Powershell 命令

Running a Powershell command with a variable -Path parameter

有人可以向我解释以下行为吗?

PS C:\Users\Kenny> $filePath = "C:\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\"
PS C:\Users\Kenny> cd $filePath
cd : Cannot find path 'C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $filePath
+ ~~~~~~~~~~~~~~~~                                 
    + CategoryInfo          : ObjectNotFound: (C:\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd 'C:\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\'
PS C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists> cd c:
PS C:\Users\Kenny> Write-Host $filePath
C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\
PS C:\Users\Kenny> cd "$filePath"
cd : Cannot find path 'C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd "$filePath"
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd ${filePath}
cd : Cannot find path 'C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd ${originalPath}
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd ${$filePath}
cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a
non-null value.
At line:1 char:1
+ cd ${$filePath}
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd $($filePath)
cd : Cannot find path 'C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $($filePath)
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

如果我在 PowerShell 坚持认为反引号、引号和其他元字符恰到好处 上浪费了一分钱,我将成为世界上最富有的人地球!有人请把我从这种疯狂中解救出来!

顺便说一句,我 实际上 试图做的是递归遍历一堆文件夹并删除 不是 的所有内容一个视频文件,像这样...

Remove-Item -Path $filePath -Recurse -Exclude '*.avi','*.mkv','*.mp4'

...由于(大概)相同的原因不起作用:无法将变量传递给 -Path 参数。如果有人觉得 真的 慷慨,他们也可以帮助我 this。 MTIA! :D

使用 -LiteralPath 而不是 -Path 因为一些特殊字符用 -Path 解释。

Remove-Item -LiteralPath

特殊角色在 PS 中非常有趣,对吧?

-LiteralPath 的工作原理与 Patrick 解释的完全一样,但它们不允许使用通配符;因为它们是字面意思。

您是否尝试过使用单引号 ' 而不是双引号 "。这允许您转义特殊字符,同时仍然评估通配符。请尝试以下命令:

New-Item -Path 'C:\Users\username\PSScripts\bracket`[\te$t.txt'
Get-Item -Path  'C:\Users\username\PSScripts\bracket`[\*'

此外,如果有帮助,我会在大多数脚本中使用 VSCode,如果您使用制表符完成,它会为您正确设置格式。

希望对您有所帮助!

我的整个问题的解决方案最终成为我的批处理脚本的这一行:

powershell -Command "Get-ChildItem -Recurse -LiteralPath %filePath% | Where-Object Extension -match '^(?!(\.avi|\.mkv|\.mp4)$)' | Remove-Item"

正则表达式是最难搞定的,因为我之前没有使用过否定前瞻,但是在花费大约 2 小时的总时间在这 一行代码 上之后,终于成功了!