What you must know about ‘Out-File’

By Jesse Hamrick • July 27th, 2007

I ran into this question today regarding why the PowerShell Out-File format was not giving an expected result.

“Hey all, I’m new to Powershell (but who isn’t, eh?), but I’ve been writing all sorts of management scripts in VBScript for years now, and I need help with a script. I have a VBScript that crawls a directory tree and, checks for files that haven’t been accessed in over a year, and then outputs the name and path of all of these files to a text file. The main reason that I’m converting it to Powershell is that PS doesn’t appear to be having trouble with foreign characters, specifically Japanese, whereas I can’t get VBScript to do anything but crash when it gets to Japanese characters.

I’m probably making this far too complicated, but the issue that I have at the moment is that when it puts the output to a text file I get the same text as I would on the screen rather than the full text. For example, if I have a particularly long path in the filename, when it gets to character 117 or so on that line it just substitutes “…” for the remainder of the name. Since this output file will be parsed by an archive process later, I actually do need the full path and filenames. Anyone know how I can make it output all of the data instead of doing a “yadda yadda yadda” to me?
At any rate, I’ve just gotten started and below is all of the code that I have so far:

$target = $args[0]
$age = $args[1]

function crawltree($folder)
$dirs = get-childitem $target -Recurse | Where {$_.psIsContainer -eq $true}
$dirs | select-object FullName -verbose | out-file -filepath results.txt


The truth is the Out-File option is working as it should. The Out-File format should look exactly the same as the console output. Take the following example:

Get-Command | Out-File -Filepath c:\output.txt

Entires in the text file will look the same as the console (Out-Host):

Out-File Graphic

O.k. so now we see the truncation issue. The ellipsis(…) at the end of the definition column, as described in the original inquiry. So, how do we get the full text? To resolve the issue we are required to format the table before sending it (pipe) to the Out-File cmdlet. Here is the code to make the change:

Get-Command | Format-Table -Wrap -AutoSize | Out-File -FilePath c:\output.txt

The -Wrap option will show all information, not truncating the definition column. -AutoSize will attempt to remove the unnecessary white space. The output.txt file now looks as follows, giving a solution to the requirement of providing a full path. No “yadda yadda yadda.”

Out-File-Format Graphic

As it was designed, PowerShell Out-file formats file contents to look like console output. If the Console output will not do, manipulate the formatting before sending to the Out-File cmdlet.

Email This Post To A Friend Email This Post To A Friend


Try this:

Get-Command | Format-Table -Wrap -AutoSize | Out-File -FilePath c:\output.txt -width 200

thats “-width 200″

actually “-width 800″ works better for Get-Command output…

Thank you!!!


Leave a Comment

« | Home | »