Powershell script to find name of group which server is part of

There are close to 15 security groups which start with "XY_*". I have couple of windows server in a CSV file and each server is member of 1 group out of those 15 groups. I need to find which server is member of which group and it's name.

can someone help me with a powershell script?

P SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Assumes the csv has a header line and a column "Name":
$prefix = 'XY_'
Import-Module ActiveDirectory
Import-Csv -Path C:\Temp\computers.csv | ForEach-Object {
	$computer = $_.Name
	Get-ADComputer -Identity $computer -Property memberOf |
		Select-Object -ExpandProperty memberOf |
		Where-Object {$_ -match "^CN=(?<Group>$($prefix).*?),(CN|DC|OU)="} |
		Select-Object @{n='ComputerName'; e={$computer}}, @{n='Group'; e={$Matches['Group']}}
} | Export-Csv -NoTypeInformation -Path C:\Temp\computergroups.csv

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
P SAuthor Commented:
Thanks oBdA. as usual you've been very helpful and script worked perfectly. Just need a small favour, would you mind explaining the regex you used and also if there is any web page that can explain the powershell regex would be very useful.

Thanks again
^CN= - match "CN=" at the beginning of the string (^)
(?<Group> - open a named group 'Group'
$($prefix).*? - match anything .* non-greedy (shortest match possible), starting with the prefix selected ...
) - [close the named group - $Matches['Group'] will contain the actual group name extracted from the DN]
,(CN|DC|OU)=" - ... until a comma followed by either CN, or DC, or OU, and an = sign.

There are hundreds of websites explaining regular expressions; they're not limited to PowerShell. There may be slight differences for the more complex constructs, but the basics apply to most of the languages that have RegEx support (just don't use findstr.exe - its RegEx support is just horrible ...)
Regular Expression Language - Quick Reference
P SAuthor Commented:
Thanks obDA. you're the best.
P SAuthor Commented:
ObDA gave a perfect script. please close this question
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.