将PowerShell输出重定向到文本文件 - 新文件与附加的结果不同

1 powershell text

我有一个奇怪的问题,将输出重定向到文件.我尝试了几种不同的方法,并继续遇到格式化问题.

我将以下代码作为完美运行的示例:

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

sort Name -Descending | format-table Name, LastWriteTime >> c:\temp\newfile.txt 
Run Code Online (Sandbox Code Playgroud)

这是文本文件中输出的样子:

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                                                               
Run Code Online (Sandbox Code Playgroud)

它完全按照我的预期工作,并希望它.但是,当我尝试做同样的事情并附加到现有文件时,我会得到奇怪的格式.使用此代码:

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

sort Name -Descending | format-table Name, LastWriteTime >> c:\temp\existingfile.txt 
Run Code Online (Sandbox Code Playgroud)

附加到现有文本文件时生成格式奇怪的结果.格式化不会在这里遇到,但这是我可以在这里显示的最接近的近似值.实际的文本文件比这里显示的更加混乱.

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 
Run Code Online (Sandbox Code Playgroud)

那么为什么会发生这种情况呢?如何在追加到现有文件时获得与新文本文件相同的结果?

Sus*_*ver 6

您正在处理不同的文件编码.将powershell的输出重定向到文件时,该文件将接收Unicode文本.

管道转出文件而不是重定向,您可以控制编码.

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.
Run Code Online (Sandbox Code Playgroud)

我不知道您要附加的文件的编码是什么,但假设它是简单的ascii,您的cmd变为:

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 
Run Code Online (Sandbox Code Playgroud)

注意:$ OutputEncoding的自动var控制PowerShell中重定向的默认编码.