0

My code currently goes through a list of folders/subdirectories looking for '.csproj' files. The properties "Include" and "Version" are taken from the found .csproj file(s) and are exported to an excel sheet. While this works and I do get two columns for each property, I need to show the name of the file that each excel row entry belongs too. I haven't been able to find an approach to this. So in my final excel sheet, I need 3 rows: Include, Version, NameOfTheFile.

Through troubleshooting I added "$xml.name" after the line "Select-Object -Property Include, Version" and this added spaces in the excel sheet between each batch of properties. That's somewhat helpful, but the name of the files would be far more sufficient.

$Directory = Get-ChildItem C:\Repos\Common\Caching\src -Directory
$ErrorFiles = [System.Collections.Generic.List[string]]::new()

$result = foreach ($d in $Directory) {
    Write-Host "Working on directory $($d.FullName)..."
    $folders = Get-ChildItem -Path $d.FullName -File -Recurse -Filter *.csproj
    foreach($folder in $folders) {
        try {
            $xml = [xml](Get-Content $folder.FullName -Raw)
            $xml.SelectNodes("/Project/ItemGroup/PackageReference") |
            Select-Object -Property Include, Version
            $xml.Name
            
        }
        catch {
            Write-Warning $_.Exception.Message
            $ErrorFiles.Add($folder.FullName)
        }
    }
}

$result | Export-Excel -Path C:\Temp\ExampleExcel.xlsx -AutoSize -AutoFilter
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
sirgroot
  • 11
  • 1

1 Answers1

0

You can use a calculated property with Select-Object, the only change you would need to do is this:

$xml.SelectNodes("/Project/ItemGroup/PackageReference") |
Select-Object -Property Include, Version
$xml.Name

For this:

$xml.SelectNodes("/Project/ItemGroup/PackageReference") |
Select-Object -Property Include, Version, @{
    Name = 'NameOfTheFile'
    Expression = { $folder.Name } # => this is a file though
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37