术语' '不被识别为cmdlet的名称

时间:2014-06-18 11:34:43

标签: powershell powershell-v2.0

我正在尝试使用“运行”命令在一行中运行它:

powershell -NoExit ; Get-ChildItem -Recurse -force -Include *.ost \\PcHostname\c$\users -ErrorAction "SilentlyContinue" | ls | Select-Object Name, @{Name="GigaBytes";Expression={$_.Length / 1GB}}

但是我收到了这个错误:

GigaBytes : The term 'GigaBytes' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:137
+ ... t Name, @{Name=GigaBytes;Expression={$_.Length / 1GB}}
+                    ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (GigaBytes:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

代码适用于没有powershell -NoExit ;的已启动的Powershell窗口。

有任何想法要解决它吗?

1 个答案:

答案 0 :(得分:3)

由于命令行参数通过“运行”标记的方式,您需要转义GigaBytes周围的双引号(使用\转义字符)或用单引号替换它们。这两个Select-Object命令中的任何一个都应该有效:

Select-Object Name, @{Name=\"GigaBytes\";Expression={$_.Length / 1GB}}

Select-Object Name, @{Name='GigaBytes';Expression={$_.Length / 1GB}}

这是因为用双引号括起的任何内容都将被解释为单个参数。

有关如何通过“运行”标记命令行参数的更详细说明,请参阅this答案。