Hi,
I’m working on a script to pick out usernames from a text file with som wonky formatting. It’s not a CSV or XML file. It has a number of (for me) useless lines above and below the content I’m interested in.
The username is two letters followed by four digits preceded by DOMAIN. Before the username on the same row there is also a computername in the same format.
I’ve got a clunky way of picking out the usernames, which consists of a number of foreach-objects massaging the output of a Select-String and it just feels off and should be much simpler. Besides I’d also need to add the names to an array to do the actual work on the names at a later stage.
This is the way it looks now:
$replaceData = Get-Content -Path File.txt
$replaceData | Select-String -Pattern 'DOMAIN\\\w{2}\d{4}' |
ForEach-Object -Process { $_.Matches } |
ForEach-Object -Process { $_.Value } |
ForEach-Object -Process { $_.Split('\')[1] }