XML and PowerShell | Disturbed Rover.

Before telling you what is PowerShell let me tell you what is a shell. In simplest terms, a shell is a user interface that gives access to various services of an operating system. A shell can be command-line based or it can include a graphical user interface(GUI).

Windows PowerShell is a shell initially developed by Microsoft for the purposes of task automation and configuration management. PowerShell is now an open-source project, and it can be installed on Windows, macOS, and Linux platforms. This shell is based on the .NET framework, and it includes a command-line shell and a scripting language.

What are the things you can do with PowerShell?

Microsoft designed Windows PowerShell as a tool that helps you automate and quickly solve a lot of tedious administration tasks. For example, you can use PowerShell to display all the USB devices installed on one or multiple computers in a network, or you can set a time-consuming task to run in the background while you do other work. You can also identify and kill processes that are not responding or filter specific information about computers in a network and export it in HTML format.

PowerShell’s capabilities allow you to simplify and automate tedious and repetitive tasks by creating scripts and combining multiple commands.

If you are a network administrator, you find that PowerShell is helpful in working with Active Directory. Given that it contains hundreds of customizable commands, which are called cmdlets, PowerShell can be helpful in making you more productive. We recommend Ed Wilson’s book – Windows PowerShell Step by Step – to learn more about it. If you desire to get a glimpse of what is new in the latest iteration of Windows PowerShell for Windows 10, you should also check this official Microsoft web page: PowerShell Documentation.

In Windows PowerShell is already installed you don't need to install it.

If you want to open Powershell in your system, this is how you can do it:-

After opening it you will see a blue screen. This means you have opened the Powershell.

Now let's come to our topic of how you can parse and update an XML file from Powershell.

I am giving you commands for each and every process:-

#XML file path:-

$xmlPath="D:\ALL LDL\all ldl only\ldl nba\EVENT DETAILS\abc.xml"

#ConvertTo-XML with InputObject

$obj=New-Object -TypeName PSObject -Property @{"Jobtitle"="Powershell Admin";"Department"="Blogging"}
$xml=ConvertTo-Xml -InputObject $obj -As Document
Get-Content $xmlPath

#ConvertTo-XML with NoTypeInformation

$xml=ConvertTo-Xml -InputObject $obj -NoTypeInformation
Get-Content $xmlPath
ii $xmlPath

#Selecting node information

$xml=ConvertTo-Xml -InputObject $obj -As Document

#Case sensitive


#ConvertTo-XML with Pipeline

$xml=New-Object -TypeName xml
$xml=Get-Process A* | ConvertTo-Xml -Depth 1
ii $xmlPath
$xml.selectNodes("Objects/Object/Property[@Name=""Description"" and @Type ""System.String""])


$xmlPath="D:\ALL LDL\all ldl only\ldl nba\EVENT DETAILS\abc.xml"
"This is a Disturbed Rover" | Export-Clixml -Path $xmlPath
Get-Content $xmlPath
Get-Date | Export-Clixml $xmlPath
Get-Content $xmlPath

#Import- Clixml

Import-Clixml $xmlPath

#Create Root Node

$xml=New-Object -TypeName XML

# Add an Attribute for the root node

$xmlRoot.SetAttribute("Description","Config file for testing")

# Create child node and set attribute

$xmlL1.SetAttribute("Description","Document Management")

# Create child of a child node and set attribute and text

$xmlL2.SetAttribute("description","Document 1")

# Save File

ii $xmlPath

# Removing nodes from xml using RemoveChild

$xml=New-Object -TypeName XML
$nodes= $xml.selectNodes("//document[@authorSurname=""Author5""]")
Foreach($item in $nodes)

# Replacing a node from xml using ReplacingChild

$xml=New-Object -TypeName XML

# Remove nodes from xml using RemoveAll

ii $xmlPath

# Getting content using Xpath Expression

Get-Content -Path $xmlPath
$xml=New-Object -TypeName XML


$xml.SelectSingleNode("//document[@index > 4]")

# Getting content using Select-Xml

$xml | Select-Xml -XPath "//document" | %{ $_.Node.InnerText }

If you love this content please give a heart and share this content. It will be helpful.

Recent Posts

See All