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?
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.
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.
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.
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 & .
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
.
Try invoking cmd /c C:\MyDirectory\MyCommand.cmd
– that should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With