locked
Run a Dos command in Powershell RRS feed

Answers

  • While it is true that you can run any old cmd command in PS, sometimes you may run into issues due to the syntax of Powershell.

    There's a good article about this:
    http://blogs.technet.com/b/josebda/archive/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.aspx
    I personally prefer using here-string with Invoke-Expression:

    $command = @'
    cmd.exe /C c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
    '@
    
    Invoke-Expression -Command:$command

    This is especially helpful when you run into some heavy quoting issues.  I can see your $ sign within double quotes may prompt PS to try interpreting it as a variable which can cause problems.

    If you've unfortunately fallen into what some call a "quoting hell", there is one way to get yourself out from it:

    $batchFileContent = @'
    @echo off
    c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
    '@
    
    $batchFileContent | Out-File -LiteralPath:"$env:TEMP\backup.cmd" -Force
    
    Invoke-Expression -Command:"$env:TEMP\backup.cmd"
    
    Remove-Item -LiteralPath:"$env:TEMP\backup.cmd" -Force

    One shortcoming of this method is it's not as easy to capture the result or errors if needed.  However if all you need is just run the command then this will work for you.

    Just from my experience.

    EDIT: If you're running PS3 then just add --% anywhere in the line and PS3 will not try to parse the line in PS syntax.
    • Edited by AverageJoeOfToronto Monday, September 9, 2013 5:26 AM additional info
    • Proposed as answer by R Jason Morgan Monday, September 9, 2013 1:44 PM
    • Marked as answer by Andy Qi Sunday, September 22, 2013 7:09 AM
    Monday, September 9, 2013 5:16 AM
  • There is a better article on Technet Wiki:

    PowerShell: Running Executables
    http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

    see the tip there on 5. The Call Operator &

    and for PowerShell 3.0  on 10. Stop-Parsing Symbol --%


    Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
    Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
    My PowerShell Blog http://www.admin-source.info
    [string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
    German ? Come to German PowerShell Forum!

    • Proposed as answer by R Jason Morgan Monday, September 9, 2013 1:44 PM
    • Marked as answer by Andy Qi Sunday, September 22, 2013 7:09 AM
    Monday, September 9, 2013 1:12 PM

All replies

  • The code you posted here isn't using Start-Process at all.  In many cases, you can run a command-line program from the PowerShell prompt exactly as you would have typed it in the cmd.exe shell.  Try it this way:

    (Note:  I slightly modified the argument after "backup"; it was missing an opening quotation mark and the text "c:\".  I haven't actually tested the command to see if this syntax is correct for ntbackup.exe, though)

    #
    c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
    #

    Sunday, September 8, 2013 5:04 PM
  • While it is true that you can run any old cmd command in PS, sometimes you may run into issues due to the syntax of Powershell.

    There's a good article about this:
    http://blogs.technet.com/b/josebda/archive/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.aspx
    I personally prefer using here-string with Invoke-Expression:

    $command = @'
    cmd.exe /C c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
    '@
    
    Invoke-Expression -Command:$command

    This is especially helpful when you run into some heavy quoting issues.  I can see your $ sign within double quotes may prompt PS to try interpreting it as a variable which can cause problems.

    If you've unfortunately fallen into what some call a "quoting hell", there is one way to get yourself out from it:

    $batchFileContent = @'
    @echo off
    c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
    '@
    
    $batchFileContent | Out-File -LiteralPath:"$env:TEMP\backup.cmd" -Force
    
    Invoke-Expression -Command:"$env:TEMP\backup.cmd"
    
    Remove-Item -LiteralPath:"$env:TEMP\backup.cmd" -Force

    One shortcoming of this method is it's not as easy to capture the result or errors if needed.  However if all you need is just run the command then this will work for you.

    Just from my experience.

    EDIT: If you're running PS3 then just add --% anywhere in the line and PS3 will not try to parse the line in PS syntax.
    • Edited by AverageJoeOfToronto Monday, September 9, 2013 5:26 AM additional info
    • Proposed as answer by R Jason Morgan Monday, September 9, 2013 1:44 PM
    • Marked as answer by Andy Qi Sunday, September 22, 2013 7:09 AM
    Monday, September 9, 2013 5:16 AM
  • There is a better article on Technet Wiki:

    PowerShell: Running Executables
    http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

    see the tip there on 5. The Call Operator &

    and for PowerShell 3.0  on 10. Stop-Parsing Symbol --%


    Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
    Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
    My PowerShell Blog http://www.admin-source.info
    [string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
    German ? Come to German PowerShell Forum!

    • Proposed as answer by R Jason Morgan Monday, September 9, 2013 1:44 PM
    • Marked as answer by Andy Qi Sunday, September 22, 2013 7:09 AM
    Monday, September 9, 2013 1:12 PM
  • Hi,

    Any update?

    Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.

    Best Regards,

    Andy Qi

    TechNet Subscriber Support

    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here

    Andy Qi
    TechNet Community Support

    Tuesday, September 17, 2013 2:38 AM
  • To keep it simple:

    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 outputing the commands to command prompt / cmd directly.

    A much safer way then running batch files!

    • Proposed as answer by GrandArchDuke Sunday, April 23, 2017 8:22 PM
    Tuesday, May 19, 2015 10:05 AM