# redirect output of cmd.exe

**URL:** <https://forums.powershell.org/t/redirect-output-of-cmd-exe/3817>\
**Category:** PowerShell Help\
**Created:** [March 2, 2015, 9:19am UTC](https://forums.powershell.org/t/redirect-output-of-cmd-exe/3817 "2015-03-02T09:19:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lewis-schwartz](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lewis-schwartz](https://forums.powershell.org/u/lewis-schwartz)\
**Post date:** [March 2, 2015, 9:19am UTC](https://forums.powershell.org/t/redirect-output-of-cmd-exe/3817/1 "2015-03-02T09:19:34Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.powershell.org/u/daniel)\
**Post date:** [March 2, 2015, 5:08pm UTC](https://forums.powershell.org/t/redirect-output-of-cmd-exe/3817/2 "2015-03-02T17:08:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/redirect-output-of-cmd-exe/3817/3 "2024-05-16T20:46:17Z")

</div>


