run powershell command from cmd

28

10

how i can run this command from cmd :

powershell.exe "(get-process | ? {$_.Description -eq "Sysinter Process Explorer"}) | select processname | out-file $env:APPDATA\example.txt"

i still get this error :

You must provide a value expression on the right-hand side of the '-eq' operato r. At line:1 char:37 + (get-process | ? {$_.Description -eq <<<< Sysinternals Process Explorer}) | select processname | out-file $env:APPDATA\example.txt + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx ception + FullyQualifiedErrorId : ExpectedValueExpression

Qassam Mahmoud

Posted 2016-05-24T10:10:59.603

Reputation: 333

3Your problem might be your inner set of quotation marks. Try either removing them or if they must be used, try using this guy: ' aka the apostrophe in place. – DeathByTensors – 2016-05-24T11:15:15.460

2Warning - please do not use obscenities in your posts. I've removed it for you. Note that other users may have flagged your post as "rude or abusive" leading to possible rep loss or suspension. Please read Be Nice: "Avoid vulgar terms and anything sexually suggestive" – DavidPostill – 2016-05-24T11:43:56.403

Answers

35

powershell -command "get-process | ? {$_.Description -eq 'Sysinter Process Explorer'} | select processname | out-file $env:APPDATA\example.txt"

basically you have a powershell command and paste it in between these quotes to call it from CMD

powershell -command " #PasteCodeHere "

inside these quotes you have to work with ' otherwise it will interrupt your command parameter.

Edit: Additional Information:

quite often you will encounter this: powershell -command "& 'somestuff'"

the & is used to call a File. when you're only using a command & is unnessecary, when you want to call a script, you should use it.

powershell -command "& 'C:\foobar.ps1'"

You could also use powershell -file C:\file.ps1 to call a script

SimonS

Posted 2016-05-24T10:10:59.603

Reputation: 4 566

u are the best man <3 it's working – Qassam Mahmoud – 2016-05-24T15:46:06.967

3If the command to be executed has quotes, they must be doubled. Example : powershell -command "dir ""c:\Program Files""" – Myobis – 2018-03-29T09:28:09.463

1@myobis or you can just use single quotes powershell -command " dir 'C:\Program Files' " – SimonS – 2018-03-29T10:06:26.863

1@myobis Actually, doubling the quotes doesn't work (in Windows 10 cmd). But using backslash escaping did: powershell -command "dir \"c:\Program Files\" " – wisbucky – 2018-04-03T20:43:44.407

0

I placed the following commands into a batch file to reset Edge (which has been giving some problems from time to time). The batch file was then run at Administrator level. Please note the triple quotes in the powershell line. This example may clarify things for those trying to run a powershell command from a "cmd" command line.

@echo off
c:
cd %userprofile%\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe
rmdir /s /q 
userprofile%\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe
powershell "Get-AppXPackage -AllUsers -Name Microsoft.MicrosoftEdge | Foreach 
{Add-AppxPackage -DisableDevelopmentMode -
  Register"""$($_.InstallLocation)\AppXManifest.xml""" -Verbose}"

Note the "triple" quotes in the Powershell line. That line by the way is a single line with "For Each" and "-Register" being word-wrapped in this comment-box. It should be a single line though in the batch file (or on the command line if manually typed into a cmd session).

The important thing is that after the word "PowerShell" inverted commas (") start and end the command and any internal inverted commas already in the powershell command being passed are converted to "triple" quotes (""")

craisin

Posted 2016-05-24T10:10:59.603

Reputation: 21