PowerShell XML

Introduced by PowerShell XML

Extensible Markup Language( XML) is a soft, light-weighted markup language like HTML that defines the situate of rules for encoding documents in a format that is both human-readable and machine-readable and used to store and transport data. This communication is self-descriptive so PowerShell squanders XML generally to retrieve the data from the webpage, revise the contents and pole it back on the site and even many work configurations are in the XML format that is easy for the application to process the data for machines and can easily be understood and modified by humans.


XML Format ๐Ÿ˜› TAGEND


Select-XML cmdlet syntax ๐Ÿ˜› TAGEND

Select-Xml [-Xml] [-Path] -LiteralPath -Content [-XPath] [-Namespace ] []

ConvertTo-XML syntax ๐Ÿ˜› TAGEND

ConvertTo-Xml [-Depth ] [-InputObject] [-NoTypeInformation][ -As ] []

XPath Syntax ๐Ÿ˜› TAGEND

This is not only for the PowerShell but it is a global XPath syntax and XPath is a major element in the XSLT standard. XPath is very helpful in retrieving the data from the XML file.

Expression Description NodeName Selects all nodes with the identify “NodeName”

/ Selects from the spring node

// Selects Nodes from the entire paper with that tag

. Hand-picked the current node

.. Select the parent of the current node

@ Select Attribute

How does XML in PowerShell Works?

PowerShell handles the XML files very efficiently. There are two ways that PowerShell retrieves the data from the XML file.

XPath Dot Method.

And for altering a datum to XML,


Before we start further explanation, we will use a Sample file( Books.xml) from Microsoft to read XML data throughout this article.

https :// docs.microsoft.com/ en-us/ previous-versions/ windows/ desktop/ ms762271( v= vs. 85 )? redirectedfrom= MSDN

1. Retrieving Data from the XML file

Loading XML file

To load the XML file into PowerShell, we can use the First Get-Content command and then typecast into the XML as shown below.

$ file=[ XML ]( Get-Content C :\ Temp \ SampleXML.xml)


[ XML ]$ datum= Get-Content C :\ Temp \ SampleXML.xml

You can also loading XML by creating an XML object type and then loading an XML file. For illustration,

$ xml= New-Object -TypeName XML $xml.Load(‘C:\Temp\SampleXML.xml’)$ xml.Load( ‘C :\ Temp \ SampleXML.xml’)

In the above cases, you will get the output as below,

Output ๐Ÿ˜› TAGEND

PowerShell XML output 1

To check which procedures are supported in XML operation, you can check approaches employing the Get-Member command as shown below.

$ xml | Get-Member

a. XPath method

To work with the XPath method, we can use the table shown in the Syntax section. First of all, to retrieve the data of the single node from the part record, we can use the below dictation syntax.

$ xml.SelectNodes( ” // NodeName”)

In this example, we have nodes like Book, Author, Title, genre, etc. We can select any of them.

$ file.SelectNodes( ‘ // genre’)

Output ๐Ÿ˜› TAGEND

PowerShell XML output 2

Please note that the XML operation is case-sensitive.

To select single node, we can use SelectSingleNode() method.

$ xml.SelectSingleNode( ‘ // title’)

This will select the data from the first node. To select a different node, we need to provide the display number. In PowerShell, the display began with[ 0] but in XML, it began with lists 1,2, 3, and so on.

$ xml.SelectSingleNode( ‘ // catalog/ volume[ 2] ‘)

The above bidding will select book number 2 from the central catalog.

Output ๐Ÿ˜› TAGEND

PowerShell XML output 3

The below node is selected.

PowerShell XML output 4

You can also immediately use the command without specifying the mother node.

$ xml.SelectSingleNode( ‘ // diary[ 2] ‘)

To use get the above output, we can also use the Select-XML command which labor the same action as the above command. For illustration,

Select-Xml -Xml$ xml -XPath ” // catalog/ book[ 2] ” | Select -ExpandProperty Node

Output ๐Ÿ˜› TAGEND

PowerShell XML output 5

You can also to expand as shown below.

$ xml.SelectSingleNode( ‘ // list/ volume[ 2 ]/ author’)

Output ๐Ÿ˜› TAGEND

PowerShell XML output 6

b. Dot method

This method is very well known that we are using on daily basis in PowerShell. We have already loaded the XML document at the beginning of this section. We will use now the Dot method for it.

The below command will retrieve work quantity 2 from the XML document.

This is our sample XML document.

PS C :\>$ xml xml list — ——- version=”1.0″ catalog Command to retrieve book number: 2 $xml.catalog.book[2]

Output ๐Ÿ˜› TAGEND

PowerShell XML output 7

2. ConvertTo the XML file

To convert any possible output to the XML, you can use the convertTo-XML command.

$ business= Get-Service | ConvertTo-Xml $services= Get-Service | ConvertTo-Xml $services

Output ๐Ÿ˜› TAGEND

output 8

To expand further, we can use any of the methods( Xpath or Dot method ). For sample,

$ services.Objects.Object[ 1] | Select -ExpandProperty Property

Output ๐Ÿ˜› TAGEND

output 9


Here are the follwoing samples mention below

1. To load the part content of the XML

To load the part XML using

$ xml=[ xml ]( Get-Content C :\ Temp \ SampleXML.xml) $xml.SelectNodes(‘/catalog’) | Select -ExpandProperty Book

Output ๐Ÿ˜› TAGEND

output 10

2. To sought for the specific property

To search the specific attribute we can use the below dictation. It will search for the book id’ bk112’.

$ xml.SelectNodes( ‘ // list/ book’) | where $ _.id -eq ‘bk1 12 ‘

Output ๐Ÿ˜› TAGEND

output 11

3. Finding quality working Dot Method

The example shown above can also be written through the Dot method as shown below.

$ xml.catalog.book | where $ _.id -eq “bk1 12 ”

Output ๐Ÿ˜› TAGEND

output 12

4. To supplemented a brand-new aspect in the folder

We will contribute the new factor to the existing file.

$ file= ‘C :\ Temp \ SampleXML.xml’ $xml=[ xml ]( Get-Content$ file)

The below require will create a “Book1” called brand-new constituent in the XML file.

$ newelement=$ xml.CreateElement( “Book1”) $newelement

Output ๐Ÿ˜› TAGEND

output 13

Once it is created, the second task is to append it to the existing document.

$ xml.catalog.AppendChild ($ newelement)

Output ๐Ÿ˜› TAGEND

output 14

Once you appended these components, next is to save the file using the below command.

$ xml.Save ($ folder)

Now let’s test if the new constituent appears in the XML paper or not.

$ xml.catalog

Output ๐Ÿ˜› TAGEND

output 15

5. Create a brand-new node and gave its dimensions

In this illustration, we will contributed a new volume name “XML Introduction” with the code shown below.

$ file= “C :\ temp \ SampleXML.xml” $xml=[ xml ]( Get-Content$ file) $newbookelement=$ xml.catalog.AppendChild ($ xml.CreateElement( “book” )) $newbookelement.SetAttribute(“id”,”bk113″)$ newAuthorelement=$ newbookelement.AppendChild ($ xml.CreateElement( “author” )) $newAuthorelement.AppendChild($xml.CreateTextNode(“Dan Harley” )) | Out-Null $newtitleelement=$ newbookelement.AppendChild ($ xml.CreateElement( “title” )) $newtitleelement.AppendChild($xml.CreateTextNode(“XML Introduction” )) | Out-Null $xml.Save(($file))

Output ๐Ÿ˜› TAGEND

output 16

You can also check the same in the XML file.

output 17


XML files are the best way to provide the configuration data. Most works use XML files for configuration and even to interact with the RestAPI programmes, numerous websites have XML as the API output.

Recommended Articles

This is a guide to PowerShell XML. Now we discuss that XML files are the best way to provide the configuration data. You may also have a look at the following articles to learn more –

PowerShell Sleep PowerShell scriptblock PowerShell Filter PowerShell Registry

The post PowerShell XML sounded first on EDUCBA.

Read more: educba.com