Finding specific text in a file using Select-String

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>.

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:

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.