Export-Csv以空白表结束

时间:2017-07-10 13:43:28

标签: powershell export-to-csv

我编写脚本来获取目录,文件名和行数,但是当我导出到CSV时,我以null表结束。怎么了?

这是我写的代码:

Get-ChildItem C:\Users\bi\Desktop\MOMO_FILES -Include *.txt -Recurse 
$result = % {
  $_ | select CreationTime, DirectoryName, name, @{n="lines";e={
    Get-Content $_ | Measure-Object -Line | select -expa lines
  }}
 }
 $result | Export-Csv C:\Users\bi\Desktop\filename.csv -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

您的问题是您没有将Get-ChildItem部分合并到您的管道中。你需要这样做:

$result = get-childitem C:\Users\bi\Desktop\MOMO_FILES -Include *.txt -recurse  | % { $_ | select CreationTime, DirectoryName,name, @{n="lines";e={
   get-content $_ | 
     measure-object -line |
         select -expa lines }
   } 
} 
$result | Export-csv C:\Users\bi\Desktop\filename.csv -NoTypeInformation

或者,如果您希望它稍微偏向水平,请将Get-ChildItem结果发送到变量并使用:

$files = get-childitem C:\Users\bi\Desktop\MOMO_FILES -Include *.txt -recurse 
$result = $files | % { $_ | select CreationTime, DirectoryName,name, @{n="lines";e={
   get-content $_ | 
     measure-object -line |
         select -expa lines }
   } 
} 
$result | Export-csv C:\Users\bi\Desktop\filename.csv -NoTypeInformation