Hello,
I’ve just started using PS (I know…) and already stuck. Need your help with some user list I’m trying to get.
I have a list of users, with each user member of different AD groups - Marketing, HR, Printing, etc. A certain “type” of group I’m interested in starts with the same words, so I can filter by that. BUT, some of the users in that list aren’t part of any of these groups. Like so:
Users Special Group
U1 Spe.Grp.1
U2 Spe.Grp.2
U3 Spe.Grp.1
U4 Spe.Grp.3
U5 Not in any group that matches the filter
U6 Spe.Grp.Test.1
U7 Not in any group that matches the filter
The problem is all the special groups are randomly named (after the Spe.Grp.*), all the users are spread inside AD in different OU’s and there are around 300 users I need this for.
I have this so far:
Import-Csv .\file.txt |% {Get-aduser $_.ppl |select samaccountname } |% {$smth=$_.samaccountname;Write-Output $_;} |% {Get-ADPrincipalGroupMembership $_.samaccountname |? {$_.name -like "Spe.Grp.*"}} |Select-Object @{n='samaccountname';e={$smth}},Name > C:\mm-u-g.txt
The result is ok, except that for the users U5 and U7 it skips them entirely and I need the output with all the rows. For U5 and U7 I’d like it to say “User doesn’t belong to any Spe.Grp.”
SamAcctN Name
U1 Spe.Grp.1
U2 Spe.Grp.2
U3 Spe.Grp.1
U4 Spe.Grp.3
U5 User doesn’t belong to any Spe.Grp.
U6 Spe.Grp.Test.1
U7 User doesn’t belong to any Spe.Grp.
Also, an error to be put in when a User isn’t found would be nice. But that’s for another post perhaps ![]()
Thank you,
Radu