Running CMD command in Powershell

I am having a bunch of issues with getting a Powershell command to run. All it is doing is running a command that would be run in CMD prompt window.

Here is the command:

"C:Program Files (x86)Microsoft Configuration ManagerAdminConsolebini386CmRcViewer.exe" PCNAME

I have tried the following with no success (I have tried many iterations of this to try and get one that works. Syntax is probably all screwed up):

$TEXT = $textbox.Text #$textbox is where the user enters the PC name.
$CMDCOMMAND = "C:Program Files (x86)Microsoft Configuration ManagerAdminConsolebini386CmRcViewer.exe"
Start-Process '"$CMDCOMMAND" $TEXT'
#iex -Command ('"C:Program Files (x86)Microsoft Configuration ManagerAdminConsolebini386CmRcViewer.exe"' $TEXT)

The command will just open SCCM remote connection window to the computer the user specifies in the text box.


Try this:

& "C:Program Files (x86)Microsoft Configuration ManagerAdminConsolebini386CmRcViewer.exe" PCNAME

To PowerShell a string "..." is just a string and PowerShell evaluates it by echoing it to the screen. To get PowerShell to execute the command whose name is in a string, you use the call operator & .


To run or convert batch files externally from powershell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a powershell script eg deletefolders.ps1

Input the following into the script:

cmd.exe /c "rd /s /q C:#TEMPtest1"

cmd.exe /c "rd /s /q C:#TEMPtest2"

cmd.exe /c "rd /s /q C:#TEMPtest3"

*Each command needs to be put on a new line calling cmd.exe again.

This script can now be signed and run from powershell outputing the commands to command prompt / cmd directly.

A much safer way then running batch files!


You must use the invoke-command cmdlet to launch this external program. Normally it works without an effort. If you need more than one command you should use the invoke-expression cmdlet with the- scriptblock option. Tell me if it's clear for you.

链接地址: http://www.djcxy.com/p/30244.html

上一篇: 在循环中运行Powershell命令时截断输出

下一篇: 在Powershell中运行CMD命令