Iterating through thousands of xml files faster in PowerShell 5

92 Views Asked by At

Below is part my code for a script that tries to find a certain number from a folder containing thousand of XML files. Only problem is, running that script takes over an hour, which is not ideal. So either I have to find a way to make the script run faster, or pre-filter the directory somehow. Any help is greatly appreciated!

This is the code I used for my script. I already tried speeding it up by using the foreachobject pipeline instead of Foreach $file in $files, and using the xml reader functionality.

$files | ForEach-Object{ # <- loop through the files

    $filename = $_.FullName

    [xml]$doc = Get-Content $filename -ReadCount 0 # <- Read content as XML 

    #$batch_name = Select-Xml -Xml $doc -Xpath "//Batch[1]/@BatchID" # <- Use xpath  to get batch id from file, assign it to variable for comparision
    $batch_name = $doc.SelectSingleNode("//Batch[1]/@BatchID").Value
    
    if ($batch_id -eq $batch_name){ #<- Compare given batch id with each file

        Write-Host "Found file '$filename' with '$batch_name'" #<- Write the full path to the file found
        $filename | Out-File $outfile -Append
    }

    Write-Progress -PercentComplete($counter*100 / $files_count) -Activity "Files searched $counter/$files_count" -Status 'Working...'  #<- Code for progress bar
    $counter++
}

0

There are 0 best solutions below