G boot
G boot

Reputation: 11

Redirecting PowerShell output to text file - different results with new file vs appending

I having a weird issue redirecting output to a file. I have tried several different ways to do it, and kept running into formatting issues.

I have the following code as an example that works perfectly:

Get-childitem -Path 'C:\Program Files' | Where-Object{$_.Name -like "*microsoft*"} | 

sort Name -Descending | format-table Name, LastWriteTime >> c:\temp\newfile.txt 

This is what the output looks like in the text file:

Name                                                                                 LastWriteTime                                                                       
----                                                                                 -------------                                                                       
Microsoft.NET                                                                        05/07/2015 7:56:29 AM                                                               
Microsoft Synchronization Services                                                   05/07/2015 7:56:39 AM                                                               
Microsoft Sync Framework                                                             05/07/2015 7:56:29 AM                                                               
Microsoft SQL Server Compact Edition                                                 05/07/2015 7:56:29 AM                                                               
Microsoft Silverlight                                                                05/26/2015 10:18:53 AM                                                              
Microsoft Policy Platform                                                            05/07/2015 7:38:56 AM                                                               
Microsoft Office                                                                     05/07/2015 5:48:43 PM                                                               
Microsoft Application Virtualization Client                                          01/27/2015 6:45:27 PM                                                               
Microsoft Analysis Services                                                          05/07/2015 7:54:46 AM                                                               

It works exactly as I would expect and want it to. However, when I try to do the same thing and append to an existing file, I get strange formatting. Using this code:

Get-childitem -Path 'C:\Program Files' | Where-Object{$_.Name -like "*microsoft*"} | 

sort Name -Descending | format-table Name, LastWriteTime >> c:\temp\existingfile.txt 

Produces strangely formatted results when appending to an existing text file. The formatting doesn't come across right here, but this is the closest approximation of what I can get to display here. The actual text file is even more messed up than can be shown here.

This is a test of appending to an existing file
-------
This is some previously logged information
------
It doesn't really matter what text is already here
------


N a m e 
   M i c r o s o f t   S y n c h r o n i z a t i o n   S e r v i c e s
      M i c r o s o f t   S i l v e r l i g h t
         M i c r o s o f t   A p p l i c a t i o n   V i r t u a l i z a t i o n  C l i e n t 

So why is this occurring, and how do I get the same results that I get in a new text file when appending to an existing file?

Upvotes: 1

Views: 1261

Answers (1)

SushiHangover
SushiHangover

Reputation: 74134

You are dealing with different file encoding. When you redirect the output of powershell to a file, the file receives Unicode text.

Instead of redirection, pipe to out-file and you can control the encoding.

Out-File [-filePath] string [[-encoding] string]
            [-append] [-width int] [-inputObject psobject]
               [-force] [-noClobber] [-whatIf]
                  [-confirm] [CommonParameters]
   -encoding string
       The character encoding used in the file. 
       "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode",
       "Default", or "OEM". The default is Unicode.
       Default=system ANSI code page. 
       OEM=OEM code page identifier for the OS.

I do not know what the encoding of the file that you are appending to, but assuming it is plain ascii, your cmd becomes:

Get-childitem -Path 'C:\Program Files' | Where-Object{$_.Name -like "*microsoft*"} | 
sort Name -Descending | format-table Name, LastWriteTime | 
out-file -append -encoding ASCII -filepath c:\temp\existingfile.txt 

Note: The automatic var of $OutputEncoding controls the default encoding of redirection in powershell.

Upvotes: 6

Related Questions