2

I'm a bit new to PowerShell. I have a working script returning -Line, -Character and -Word to a csv file. I can't figure out how to add the full name of the file into the 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

I've tried using Write-Host and Select-Object, but I'm not sure about the syntax.

I've been using the following as a reference.

Results

screenshot

This is what I'm after

solution

Community
  • 1
  • 1
aduguid
  • 3,099
  • 6
  • 18
  • 37

1 Answers1

2

Use Select-Object with a calculated 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

Note:

  • Pass -ExcludeProperty Property to Select-Object to omit the empty Property column.

  • Pass -NoTypeInformation to Export-Csv to suppress the virtually useless first line (the type annotation) in the CSV.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    I now have a much more complete script in [Code Review](https://codereview.stackexchange.com/q/222458/158032) – aduguid Jun 19 '19 at 12:26