Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a CMD prompt window.

Here is the command:

"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.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 Manager\AdminConsole\bin\i386\CmRcViewer.exe" Start-Process '"$CMDCOMMAND" $TEXT' #iex -Command ('"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"' $TEXT) 

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

like image 372
user3585839 Avatar asked Jul 24 '14 17:07

user3585839


People also ask

Can I run CMD commands in PowerShell?

Many legacy Command Prompt (CMD) commands work in the Windows PowerShell scripting environment. The PowerShell environment carries these commands forward from the most used commands like ping to the most informational commands like tracert from the legacy environment using aliases.

How do I run a command in Windows PowerShell?

Run Your PowerShell Scripts. After configuring the execution policy, you can run PowerShell scripts. To run a script, open a PowerShell window, type the script's name (with or without the . ps1 extension) followed by the script's parameters (if any), and press Enter.

How do I run a .CMD file in PowerShell script?

*Each command needs to be put on a new line, calling cmd.exe again. This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly. It is a much safer way than running batch files!

Is PowerShell same as CMD?

PowerShell is a more advanced version of the cmd used to run external programs like ping or copy and automate many different system administration tasks which are not accessible from cmd.exe. It's quite similar to cmd except it's more powerful and uses different commands altogether.


2 Answers

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, e.g. deletefolders.ps1.

Input the following into the script:

cmd.exe /c "rd /s /q C:\#TEMP\test1"  cmd.exe /c "rd /s /q C:\#TEMP\test2"  cmd.exe /c "rd /s /q C:\#TEMP\test3" 

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

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

It is a much safer way than running batch files!

like image 41
Armand G. Avatar answered Oct 09 '22 09:10

Armand G.


Try this:

& "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.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 &.

like image 170
Keith Hill Avatar answered Oct 09 '22 10:10

Keith Hill