Search code examples
windows-10powershell-5.0

Export CSV Writes Results to Single Line, How Do I get Multiple Lines?


I am looking for duplicates on a share drive so I can let the users know and they can clean it up before we use anything automated. My largest duplicate is close to 400 copies, but the info is all on a single line.

My query is getting the correct results:

$a = Get-ChildItem -Path "S:\" -File -Recurse | 
     Select-Object -Property Fullname, @{N='Hash';E={(Get-FileHash $_.FullName).Hash}}

$cnt = $a | Group-Object -Property Hash

$cnt |
    Select-Object Count, @{N='FullName';E={($_.Group).FullName}}, @{N='Hash';E={($_.Group).Hash}} |
    Sort-Object -Property Count -Descending |
    Export-Csv C:\Temp\S_Drive_Counts.csv

Here is an example of my results where each entry is on a single line:

"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83 498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"

I need to pass this info on though, so I'd like to have each entry on a line by itself, similar to this:

"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
"2","S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"

I can do some string manipulation to the CSV if needed, but I am looking for a way to get it in the correct format before exporting to the CSV.


Solution

  • Unroll your groups. Also, make better use of the pipeline.

    Get-ChildItem -Path 'S:\' -File -Recurse | 
        Select-Object Fullname, @{n='Hash';e={(Get-FileHash $_.FullName).Hash}} |
        Group-Object Hash |
        ForEach-Object {
            $cnt = $_.Count
            $_.Group | Select-Object @{n='Count';e={$cnt}}, FullName, Hash
        } | 
        Sort-Object Count, Hash, FullName -Descending |
        Export-Csv 'C:\Temp\S_Drive_Counts.csv' -NoType