windowspowershellcmd

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?


Solution

  • 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.