如何使用测量对象属性返回全名

时间:2019-06-17 01:37:01

标签: powershell

我对PowerShell有点陌生。我有一个工作脚本,将-Line-Character-Word返回到一个csv文件。我不知道如何将文件的全名添加到csv中。

get-childitem -recurse -Path C:\Temp\*.* | foreach-object { $name = $_.FullName; get-content $name | Measure-Object -Line -Character -Word} | Export-Csv -Path C:\Temp\FileAttributes.csv

我尝试使用Write-HostSelect-Object,但是我不确定语法。

我一直将以下内容用作reference

结果

screenshot

这就是我的追求

solution

1 个答案:

答案 0 :(得分:2)

Select-Objectcalculated property一起使用:

Get-Childitem -recurse -Path C:\Temp\*.* | ForEach-Object { 
  $fullName = $_.FullName
  Get-Content $fullName | Measure-Object -Line -Character -Word |
    Select-Object @{ Name = 'FullName'; Expression={ $fullName } }, *
} | Export-Csv -Path C:\Temp\FileAttributes.csv

注意:

  • -ExcludeProperty Property传递到Select-Object以省略空的Property列。

  • -NoTypeInformation传递到Export-Csv,以抑制CSV中实际上无用的第一行(类型注释)。