locked
How to run .cmd file from a Powershell script RRS feed

  • Question

  • Hi,

    I would like to know how I run commands from a powershell script? how to use the invoke-command?

    This powershell script doesnt work for me

    Invoke-Command C:\setup.cmd -ArgumentList install-setupforced



    • Edited by Spade Maverick Friday, March 21, 2014 1:34 PM changing the script
    Friday, March 21, 2014 1:32 PM

Answers

  • To run a batch file from powershell, just type its name with arguments. I'd recommend explicitly including the .CMD file type and, if it is located in the current folder, powershell rules require you to explicitly say so, i.e.:

       ./mybatch.cmd inputfile.txt outputfile.txt

    To run a single cmd.exe command, just prefix it with cmd.exe /c as Mike says:

        cmd.exe /c color 48

    to run a series of "&"- separated cmd.exe commands you need to enclose the complete command string in double quotes:

        cmd.exe /c "color 37 & dir"

    Beyond that it can get complicated due to various characters that are special to powershell and those special to cmd.exe. If you need to run a series of cmd.exe commands, your best short term bet is to put them in a batch file. The best long-term bet is to move more completely to the powershell way of doing things...


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Proposed as answer by Mike Laughlin Friday, March 21, 2014 8:41 PM
    • Marked as answer by AnnaWY Tuesday, April 8, 2014 5:12 AM
    Friday, March 21, 2014 8:30 PM

All replies

  • Hi,

    Invoke-Command isn't used that way, it's meant for running commands on remote machines. Have you tried just running your command directly in the console? Alternatively, you can try the cmd /c blah blah blah workaround.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Friday, March 21, 2014 1:44 PM
  • To run a batch file from powershell, just type its name with arguments. I'd recommend explicitly including the .CMD file type and, if it is located in the current folder, powershell rules require you to explicitly say so, i.e.:

       ./mybatch.cmd inputfile.txt outputfile.txt

    To run a single cmd.exe command, just prefix it with cmd.exe /c as Mike says:

        cmd.exe /c color 48

    to run a series of "&"- separated cmd.exe commands you need to enclose the complete command string in double quotes:

        cmd.exe /c "color 37 & dir"

    Beyond that it can get complicated due to various characters that are special to powershell and those special to cmd.exe. If you need to run a series of cmd.exe commands, your best short term bet is to put them in a batch file. The best long-term bet is to move more completely to the powershell way of doing things...


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Proposed as answer by Mike Laughlin Friday, March 21, 2014 8:41 PM
    • Marked as answer by AnnaWY Tuesday, April 8, 2014 5:12 AM
    Friday, March 21, 2014 8:30 PM
  • Here is a pattern to follow that should be very bulletproof and easy to code

    cmd.exe ('/c','ver','&','echo','abc')
    Here it is executing two commands, first ver and then echo abc

    Monday, March 24, 2014 3:33 PM
  • Here is a pattern to follow that should be very bulletproof and easy to code

    cmd.exe ('/c','ver','&','echo','abc')
    Here it is executing two commands, first ver and then echo abc

    Interesting, but somewhat non-intuitive. In what way do you suggest that is simpler for coding than this somewhat more intuitive example:

    M:\>PS M:\> cmd.exe /c "ver & echo abc"
    
    Microsoft Windows [Version 6.1.7601]
    abc
    PS M:\>


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, March 24, 2014 4:18 PM
  • Essentially, my technique is the same as shown in this article

    http://s1403.t.en25.com/e/es.aspx?s=1403&e=158153&elq=50a06b56b5c047698f835351acd90b79

    Tuesday, March 25, 2014 2:35 AM
  • Essentially, my technique is the same as shown in this article

    http://s1403.t.en25.com/e/es.aspx?s=1403&e=158153&elq=50a06b56b5c047698f835351acd90b79

    Interesting, thanks.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Tuesday, March 25, 2014 3:19 AM