redirect output of cmd.exe

Welcome Forums General PowerShell Q&A redirect output of cmd.exe

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 years, 9 months ago.

  • Author
    Posts
  • #22943

    Participant
    Points: 0
    Rank: Member

    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

      {content}

    >> $lgfile

      {content}

    Out-file -filepath $lgfile [no output]

      {content}

    `$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

    Participant
    Points: 0
    Rank: Member

    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
    

The topic ‘redirect output of cmd.exe’ is closed to new replies.