XML Data and PowerShell
Now that you’ve created some XML files, let’s see how you can use them in PowerShell. If you’re just jumping into the series now, I strongly encourage you to at least skim the previous articles. I want to demonstrate how to bring XML files into PowerShell and I will be using some of the files created earlier.
The easiest way to bring an XML file to life in PowerShell assumes you planned ahead and used the Export-Clixml cmdlet. This command is designed to serialize the pipeline output of a PowerShell expression and save the results to an XML file. This file is designed to be used within another PowerShell session. In a previous article, I ran this command.
Get-Service wuauserv -comp chi-dc04,chi-p50,chi-core01 | Export-Clixml -Path c:\work\wu.xml
The resulting XML file can be re-imported into any PowerShell session using Import-Clixml. This cmdlet will deserialize the contents of the file and in essence recreate the objects.
The output is the same as it was when I originally ran the command. Using this set of commands can be very handy. I could send you the XML file and you could import it on your computer allowing you to see the results as if you had run the command on my computer. Although there is one significant difference. Look at the objects after they are imported.
The first thing to notice is the type name. This is no longer a System.ServiceProcess.ServiceController object, but a deserialized version. The property names are all the same but there are no object methods such as Start() or Stop(). The few methods listed, which I’ve crossed out, are added by PowerShell for every object type. But because the properties are the same, I can use them.
Remember, the XML file is essentially a point-in-time view of these services at the time of export. Something you might consider is adding a property when you export the data that will indicate when it was exported.
Get-Service wuauserv -comp chi-dc04,chi-p50,chi-core01 | Add-Member -MemberType NoteProperty -Name Exported -Value (Get-Date) -PassThru | Export-clixml -Path c:\work\wu-dated.xml
This will add a new property to the object, which I can use if I want.
Depending on your version of PowerShell, you can also use Export-Clixml and Import-Clixml to safely store credentials.
$cred = Get-Credential myCompany\Administrator $cred | Export-Clixml c:\work\admin.xml
The XML file does not store the password in plain text.
You can re-import this file at any time on the original computer.
$impCred = Import-Clixml c:\work\admin.xml
The passwords are the same!
This should be a safe technique, provided you limit access to your computer. If someone tries to import the file on another computer, it will fail.
I copied the XML file to the Work folder on another machine, which can’t decrypt the password. However, the username is still accessible, so I would continue to urge caution.
Learning how to use these cmdlets is very important in your PowerShell work. They will help you out in troubleshooting because you can compare a previous state, exported to a file, against the current state.
Get-Process | Add-Member -MemberType NoteProperty -Name Exported -Value (Get-Date) -PassThru | Export-clixml c:\work\proc.xml
At some point in time, I create a reference file. Later, I can compare these results with current file.
$current = Get-Process $impProc = import-clixml C:\work\proc.xml compare-object -ReferenceObject $impProc -DifferenceObject $current -Property Name
For processes, which change constantly, all I want to see is the name of processes that are running now that weren’t at the time of the import.
Of course, you may have other XML files that you want to work with in PowerShell that were not created by Export-Clixml. These files require a different set of steps, which I’ll cover next time.
More in PowerShell
How to Use PowerShell For Loop, While Loop, and Other Loops
May 23, 2022 | Ivan Mirchev
How to Check Your PowerShell Version
May 4, 2022 | Michael Reinders
How to Download a File using PowerShell
Apr 21, 2022 | Michael Reinders
How to Write and Run a PowerShell Script File on Windows 11
Apr 4, 2022 | Ivan Mirchev
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Get-AdUser: How to Audit Active Directory Users with PowerShell
Mar 3, 2022 | Michael Reinders
Most popular on petri