Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a .cmd file through PowerShell

I am trying to run a .cmd file on a remote server with PowerShell.

In my .ps1 script I have tried this:

C:\MyDirectory\MyCommand.cmd 

It results in this error:

C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet, function, script file, or operable program. 

And this

Invoke-Command C:\MyDirectory\MyCommand.cmd 

results in this error:

Invoke-Command : Parameter set cannot be resolved using the specified named parameters. 

I do not need to pass any parameters to the PowerShell script. What is the correct syntax that I am looking for?

like image 804
Declan McNulty Avatar asked Dec 06 '13 12:12

Declan McNulty


People also ask

How do I run a .cmd file?

You can run the commands stored in a CMD file in Windows by double-clicking the file or executing it in the Command Prompt (CMD. EXE) utility. You cannot run CMD files in COMMAND.COM, like you may do with BAT files, so that you do not incorrectly execute commands in the wrong Windows environment.

Can you run BAT files in PowerShell?

It is the latest shell and scripting tool for Windows and a more advanced cmd version. To run a . bat file from a PowerShell script, you can run it manually from the PowerShell.

How do I run a file in PowerShell?

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How do I run a DOS command in PowerShell?

To get PowerShell to execute the command whose name is in a string, you use the call operator & . Yes but it is not necessary for exes that don't have a space in their name or path. For instance, you can execute ipconfig.exe without having to use & .


2 Answers

Invoke-Item will look up the default handler for the file type and tell it to run it.

It's basically the same as double-clicking the file in Explorer, or using start.exe.

like image 170
alroc Avatar answered Sep 30 '22 06:09

alroc


Try invoking cmd /c C:\MyDirectory\MyCommand.cmd – that should work.

like image 27
mirabilos Avatar answered Sep 30 '22 07:09

mirabilos