
close
close
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.
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.
$cred = Get-Credential myCompany\Administrator $cred | Export-Clixml c:\work\admin.xml
The XML file does not store the password in plain text.
$impCred = Import-Clixml c:\work\admin.xml
The passwords are the same!
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.
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri