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
$xml.Save($xmlPath)
Get-Content $xmlPath
#ConvertTo-XML with NoTypeInformation
$xml=ConvertTo-Xml -InputObject $obj -NoTypeInformation
$xml.Save($xmlPath)
Get-Content $xmlPath
ii $xmlPath
#Selecting node information
$xml.Objects.Object.Property.Name
$xml=ConvertTo-Xml -InputObject $obj -As Document
$xml.Objects.Object.Property.Type
$xml.Objects.Object.Property.InnerText
$xml.Objects.Object.Property.'#text'
#Case sensitive
$xml.SelectNodes("Objects/Object/Property")
$xml.SelectNodes(Objects/Object/Property[@Name=""Jobtitle""]")
#ConvertTo-XML with Pipeline
$xml=New-Object -TypeName xml
$xml=Get-Process A* | ConvertTo-Xml -Depth 1
$xml.Save($xmlpPath)
ii $xmlPath
$xml.Objects.Object.Property.Name
$xml.selectNodes("Objects/Object/Property[@Name=""Description"" and @Type ""System.String""])
#Export-Clixml
$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
$xmlRoot=$xml.CreateElement("Config")
$xml.appendChild($xmlRoot)
# Add an Attribute for the root node
$xmlRoot.SetAttribute("Description","Config file for testing")
$xml.Config.Description
# Create child node and set attribute
$xmlL1=$xmlRoot.AppendChild($xml.CreateElement("System"))
$xmlL1.SetAttribute("Description","Document Management")
# Create child of a child node and set attribute and text
$xmlL2=$xmlL1.AppendChild($xml.CreateElement("Document"))
$xmlL2.SetAttribute("authorFirstName","First1")
$xmlL2.SetAttribute("authorSurname","Author1")
$xmlL2.SetAttribute("date",$(get-date))
$xmlL2.SetAttribute("description","Document 1")
$xmlL2.SetAttribute("index","3")
# Save File
$xml.Save($xmlPath)
ii $xmlPath
# Removing nodes from xml using RemoveChild
$xmlPath=""
$xml=New-Object -TypeName XML
$xml.Load($xmlPath)
$xml.config.system.document.authorSurname
$nodes= $xml.selectNodes("//document[@authorSurname=""Author5""]")
Foreach($item in $nodes)
{
$item.ParentNode.RemoveChild($item)
}
$xml.config.Document.authorSurname
# Replacing a node from xml using ReplacingChild
$xmlPath=""
$xml=New-Object -TypeName XML
$xml.Load($xmlPath)
$oldNode=$xml.SelectSingleNode("//document[last()]")
$oldNode
$newNode=$xml.CreateElement("document")
$newNode.SetAttribute("authorFirstName","Heiko")
$xml.config.System.ReplaceChild($newNode,$oldNode)
$xml.SelectsingleNode("//document[last()]")
# Remove nodes from xml using RemoveAll
$xmlPath=""
$xml.ChildNodes.RemoveAll()
$xml.Save($xmlPath)
ii $xmlPath
# Getting content using Xpath Expression
$xmlPath=""
Get-Content -Path $xmlPath
$xml=New-Object -TypeName XML
$xml.Load($xmlPath)
$xml.SelectSingleNode("//document[3]")
$xml.SelectSingleNode("//document[last()-1]")
$xml.SelectSingleNode("//document[3]").GetAttribute("authorFirstName")
$xml.SelectSingleNode("//document[@index > 4]")
$xml.SelectSingleNode("//document[contains(@authorSurname,""Aut"")]")
$xml.SelectSingleNode("//document[starts-with(@date,""201302"")]")
# Getting content using Select-Xml
$xml | Select-Xml -XPath "//document" | %{ $_.Node.InnerText }
Comments