0

The script below does not produce the path to the files in question, in my CSV file. Instead it is giving me System.Object[]. Please advise as to what I am doing wrong here.

My goal is to clean up redundant GPO's. I need to search the xml files from a GPO backup and analyze them. If I find the same custom string in two or more GPO's, the path will show me which folder contains the redundant string and can either combine the GPO's or delete one all together. Makes sense .. I hope?

$ht = @()
$files = Get-ChildItem -recurse -Filter *.xml
    foreach ($file in $files)
{
    $path = $file.FullName
    $lines = Get-Content $path
    foreach ($line in $lines)
    {
         if ($match = $ht | where { $_.line -EQ $line })
         {
            $match.count = $match.count + 1
            $match.Paths += $path
         }
        else
        {
            $ht += new-object PSObject -Property @{
                Count = 1
                Paths = @(, $path)
                Line = $line
            }
        }
    }
}

$ht
$ht.GetEnumerator() | select Count, Paths, Line | Export-Csv c:\temp    \NLG_GPO_Sort.csv
  • 1
    You might pipe the Get-ChildItem to Select -expandproperty fullname. – Davidw May 15 '15 at 17:58
1

Export-Csv needs to output a string to the CSV file, and since the Paths property is not a string, PowerShell implicitly calls ToString(), resulting in the string System.Object[] (the type of Paths).

Use Select-Object to calculate a semicolon-separated string of the paths, like so:

$ht.GetEnumerator() | select Count, @{Name="Paths";e={$_.Paths -join ";"}}, Line|Export-Csv C:\temp\file.csv

That being said, what you're trying to do can easily be achieved in a single pipeline using Group-Object:

Get-ChildItem -Recurse -Filter *.xml |Select-Object -First 10 |Select-String ".*" |Group-Object Line |Select-Object Count,@{Name="Paths";Expression={$_.Group.Filename -join ";"}},@{Name="Line",Expression={$_.Name}}

A little more readable:

$Lines = Get-ChildItem -Recurse -Filter *.xml |Select-Object -First 10 |Select-String ".*"
$Groups = $Lines |Group-Object Line 
$Output = $Groups |Select-Object Count,@{Name="Paths";Expression={$_.Group.Filename -join ";"}},@{Name="Line",Expression={$_.Name}}
$Output | Export-Csv C:\temp\file.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.