Unable to pipe cmdlet objects in cmd

不打扰是莪最后的温柔 提交于 2020-04-30 07:34:40

问题


i am using this powershell command to fetch a particular user profile

"Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"

But when i am using this same command in cmd by invoking powershell i am getting 'Where-Object is not recognized as an internal or external command,operable program or batch file'

The command i am running in cmd is as follows :- "powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"

I need to run this command from cmd only , i don't have any other options . So please give me an alternative to "Where-Object"


回答1:


So please give me an alternative to "Where-Object"

powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela

You don't need an alternative. The above command is failing because the pipe | is being interpreted by the cmd shell and not by PowerShell.

If you escape the pipe ^| then the piping is done by the PowerShell command as expected:

powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\Pela

Example:

F:\test>powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\DavidPostill'}

__GENUS           : 2
__CLASS           : Win32_UserProfile
__SUPERCLASS      :
__DYNASTY         : Win32_UserProfile
__RELPATH         : Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
__PROPERTY_COUNT  : 12
__DERIVATION      : {}
__SERVER          : HAL
__NAMESPACE       : root\cimv2
__PATH            : \\HAL\root\cimv2:Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
LastDownloadTime  :
LastUploadTime    :
LastUseTime       : 20160822200129.697000+000
Loaded            : True
LocalPath         : C:\Users\DavidPostill
RefCount          : 146
RoamingConfigured : False
RoamingPath       :
RoamingPreference :
SID               : S-1-5-21-1699878757-1063190524-3119395976-1000
Special           : False
Status            : 0
PSComputerName    : HAL

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • syntax - Escape Characters, Delimiters and Quotes.


来源:https://stackoverflow.com/questions/39085687/unable-to-pipe-cmdlet-objects-in-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!