redirect output of cmd.exe

This topic contains 1 reply, has 2 voices, and was last updated by  Daniël 3 years, 3 months ago.

  • Author
  • #22943

    Lewis Schwartz

    I am using Powershell 2009 on a WIN-Server 2008 R2

    I have seen several suggestions on how to "redirect" the output generated by a 'cmd.exe /c', but none of the suggested solutions were successful.

    I have a simple cmd

    cmd.exe /c my_program.cmd $file.basename

    This process creates a lot of screen output and this command is in a loop of all files in a directory.

    I want to capture the output to a file "$lgfile"

    I have used
    [ul]{content}[/ul] >> $lgfile
    [ul]{content}[/ul] Out-file -filepath $lgfile [no output]
    [ul]{content}[/ul] `$lgfile 2`>>`&1 [error '&" is resereved for future use]

    I would be grateful for any suggestions
    Also, the reason I am using "cmd.exe /c" is because that is the only method i have been able to use successfully (my_program.cmd is calling an ORACLE compiler! and all other methods to execute this compiler did not work except as a cmd-file)

  • #22946


    Use Start-Process with the RedirectStandardOutput switch, for example:

    $MyOutputFile = C:\MyOutputFile.txt
    Start-Process -FilePath c:\windows\system32\cmd.exe -ArgumentList '/c C:\YourCommand.bat' -Wait -NoNewWindow -RedirectStandardOutput $MyOutputFile

You must be logged in to reply to this topic.