foreach -parallel is by far the slowest parallelization option you have with PowerShell, since Workflows are not designed for speed, but for long-running operations that can be safely interrupted and resumed.
Implementing these security mechanisms leads to some overhead, so your script runs slower when working as a workflow.
If you want to optimize execution speed, use spaces instead:
$TestDir = "E:\Powershell\Test" $TestXMLs = Get-ChildItem $TestDir -Recurse -Include *.xml
Quick XML Processing Bonus Tips:
As wOxxOm suggests , using Xml.Load() is faster than using Get-Content to read in an XML document.
In addition, using dot notation ( $xml.root.servers.server ) and the Where({}) extension Where({}) method will also be painfully slow if there are many servers or server nodes. Use the SelectNodes() method with an XPath expression to search for "Server1" instead (remember that XPath is case sensitive):
$PSInstance = [powershell]::Create().AddScript({ param($XMLPath) $XML = New-Object Xml $XML.Load($XMLPath) $Server1Node = $XML.SelectNodes('/root/servers/server[@name = "Server1"]') return $Server1Node.serverid }).AddParameter('XMLPath', $TestXML.FullName)
source share