We have all been in situations where we need to find a specific piece of text in a file. This can be quite time consuming when you have a ton of files in a multitude of locations.
Below is a sample XML file (sample.xml) that contains a password I need to retrieve. As we can see from the file, the password is located on a line <S N=”ms-Mcs-AdmPwd”>SysetmPassword101</S>.
<Objs Version="18.104.22.168" xmlns="http://schemas.microsoft.com/powershell/2004/04">
We can use the ‘AdmPwd’ part of this line to identify what to search for.
To select the line in question we can use the following PowerShell command:
$line = (Select-String -Path "c:\xmlfiles\sample.xml" -pattern AdmPwd).ToString()
This will return the following infromation:
c:\xmlfiles\sample.xml:11: <S N=”ms-Mcs-AdmPwd”>SystemPassword101</S>
Now we can split this information on the ‘<‘ and ‘>’ symbols.
$password = $line.Split(">").split("<")
Depending on the structure of your file or the line that contains the information your need, this split can be modified.
As always, have fun playing with this, and feel free to send on your comments.
For more PowerShell scripts check out the full list.