Working with Active Directory export script. Currently, the script easily exports user objects from a given group into a .txt file as fully qualified names (ie. instead of reporting as “username”, you get the ugly “CN=username,OU=foo,OU=bar,DC=foo,DC=bar”).
My Powershell experience is novice at best. What I’ve gotten so far:
- load contents of text file
- split contents of text file at the “,”
-
write these contents into a second text file.
-
resulting text file looks like this:
"CN=username OU=foo OU=bar DC=foo DC=bar"
My goal:
- Save any resulting line of text that begins with “CN= (yes I’d like to keep that ” at the beginning)
- Delete everything else
- Strip “CN= from the beginning of the resulting lines
- Note: usernames vary in length
What I have so far:
$a = (Get-Content c:\test.txt | foreach-Object {$_.split(",,") })
Out-File c:\results.txt -inputObject $a
I’m completely lost as to how to search text files for a pattern with a wildcard (ie. seach for any lines that contain “CN=* and keep those lines and those lines only) and then stick the results into a .txt.
Thank you so much for any and all guidance you might provide.
Select-Stringwould allow that. It’s kindagrepfor PowerShell.However, if I understand you correctly you can do that a bit easier with
The comparison operators can be applied to a list of objects and return only the matching items.