1

I have a Powershell script that queries an xml document and outputs the results to a csv file. The script works but I need to apply it to multiple xml files in a folder and output the combined results to a csv. How can this script be modified to do this? Thanks

$xml = [XML](Get-Content D:demo\test.xml) #load xml document

#this finds file names of yearbook picks
$picks = $xml.Client.Order.Ordered_Items.Ordered_Item |
    Where-Object { $_.Description -eq 'yearbook Luster Print' } |
        ForEach-Object { $_.Images.Image_Name }


# this finds the Album
$album = $xml.SelectSingleNode("//Album_ID").InnerText -split '_'

$results = New-Object PSObject -Property @{
      Last= $album[0]
      First= $album[1]
      Code= $album[2]
      Pick1= $picks[0]
      Pick2= $picks[1]
    }

#output CSV File
$results | Export-Csv -path D:\demo\myoutput.csv -NoTypeInformation

1 Answer 1

1

Loop over the XML files and append them to your output file:

# Get all XML files
$items = Get-ChildItem *.xml

# Loop over them and append them to the document
foreach ($item in $items) {
    $xml = [XML](Get-Content $item) #load xml document

    #this finds file names of yearbook picks
    $picks = $xml.Client.Order.Ordered_Items.Ordered_Item |
        Where-Object { $_.Description -eq 'yearbook Luster Print' } |
            ForEach-Object { $_.Images.Image_Name }

    # this finds the Album
    $album = $xml.SelectSingleNode("//Album_ID").InnerText -split '_'

    $results = New-Object PSObject -Property @{
          Last= $album[0]
          First= $album[1]
          Code= $album[2]
          Pick1= $picks[0]
          Pick2= $picks[1]
        }

    # Append to CSV File
    $results | Export-Csv -path D:\demo\myoutput.csv -NoTypeInformation -Append
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.