PowerShell XML Output PowerShell XML Output powershell powershell

PowerShell XML Output


Since you are selecting both the objects together applying the where condition, so for maintaining the equality as a single object, it is showing like that .

In your case what you can do is to store the resultant in arraylist and do the manipulation there for all the Commands what you are getting like :

$file = "C:\folder\test.xml"$Xml  = [xml](Get-Content $file)$URI= $Xml.SelectNodes("//*").ChildNodes | Select URI| Where-Object { $_.URI -ne $null}$Command= $Xml.SelectNodes("//*").ChildNodes | Select Command| Where-Object { $_.Command -ne $null}$arraylisttable = New-Object System.Collections.ArrayList$arraylisttable.Add($URI)  | Out-Nullforeach($cmd in $Command){$arraylisttable.Add($Cmd) | Out-Null}$arraylisttable | fl

OUTPUT:

Result

Hope it helps.


$Xml.SelectNodes("//*[local-name()='Command' or local-name()='URI']") | select Name, "#text"Name    #text----    -----URI     \_Test\TestCommand C:\Folder\process1.exeCommand C:\Folder\process2.exe

This way you move selection logic to Xpath. Things to note:

  • //* selects all nodes
  • text inside [] is an XPath filter criteria.
  • using local-name() XPath function is a trick to avoid messing with namespaces

Note that XPath comparisons are case sensitive. Unfortunately XPath 1.0 does not have lower-case() or the like function. To make case-insensitive comparison (as Powershell does) use translate() function:

$Xml.SelectNodes("//*[translate(local-name(), 'COMAND', 'comand')='command' or translate(local-name(), 'uri', 'URI')='URI']") | select Name, "#text"

If you prefer pure Powershell over XPath here is alternate solution:

$xml.SelectNodes('//*') | ? {$_.Name -in ('command', 'uri')} | select Name, "#text"

One more thing about tasks. You do not have to dump them. They natively reside as xml already in theC:\Windows\Tasks orC:\Windows\System32\Tasks folders depending on the OS. For Win7 it is C:\Windows\System32\Tasks. You would need admin access to read them.

Edit: To get exactly the same output:

$xml.SelectNodes('//*') | ? { $_.Name -in ('command', 'uri') } | select Name, @{Name = 'Value'; Expression = {": $($_.'#text')" } } | ft -HideTableHeadersURI     : \_Test\TestCommand : C:\Folder\process1.exeCommand : C:\Folder\process2.exe

Custom object:

$customObject = [pscustomObject]@{    URI = ($xml.SelectNodes("//*[local-name()='URI']")).'#text'    Command = ($xml.SelectNodes("//*[local-name()='Command']")).'#text'}$customObject | flURI     : \_Test\TestCommand : {C:\Folder\process1.exe, C:\Folder\process2.exe}