1

I have a Power Shell 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, then output the results as a separate csv for each XML with the same XML name. How can this script be modified to do this?

PROCESS 
{ 
$xml = [xml](Get-Content .\sample.xml) #Sample XML

$xml.log.event | Select-object @(
@{l="times";e={$_.time}},
@{l="type";e={$_.type}},
@{l="user";e={$_.user}},
@{l="place";e={$_.place}},
@{l="message";e={$_.message}}) |
Export-Csv test.csv -NoTypeInformation
}
2
  • Do you have a directory of xml files or something? Commented Oct 30, 2017 at 23:25
  • Hi.. Yes I've got a directory with multiple xml files in it Commented Oct 30, 2017 at 23:39

1 Answer 1

2

Assuming a directory:

$ToProcess = Get-ChildItem -Path 'C:\Temp' -Filter '*.xml'

ForEach ($File in $ToProcess)
{
    [Xml]$Xml = Get-Content -Path $File.FullName

    $Xml.log.event | Select-Object -Property @(
        @{Name='Times';Expression={$_.Time}},
        @{Name='Type'; Expression={$_.Type}},
        @{Name='User'; Expression={$_.User}},
        @{Name='Place';Expression={$_.Place}},
        @{Name='Message';Expression={$_.Message}}
    ) | Export-Csv -Path "C:\Temp\$($File.BaseName).csv" -NoTypeInformation
}
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.