Loyall
asked on
Powershell: Find users who are member of groups with certain name
Hi,
I'm working at some company where a lot, about 50%, of the global security groupnames start with: "GGCTX-"
I would like to have a powershell script which I can feed a CSV file with usernames and that gives me only the groupnames that starts with "GGCTX" the user is member of.
I'm a lousy scripter. Scripts i've found on the internet that looked promising and that I tried to adjust did not do the trick and gave me at best a screen full of garble.
Is there anyone who can help me on this, or at least give me a kick into the right direction ?
Thanks
I'm working at some company where a lot, about 50%, of the global security groupnames start with: "GGCTX-"
I would like to have a powershell script which I can feed a CSV file with usernames and that gives me only the groupnames that starts with "GGCTX" the user is member of.
I'm a lousy scripter. Scripts i've found on the internet that looked promising and that I tried to adjust did not do the trick and gave me at best a screen full of garble.
Is there anyone who can help me on this, or at least give me a kick into the right direction ?
Thanks
ASKER
Hi, MaheshPM
Thank tou for your comment.
I already found a solution like yours, but I would like to have a script that only gives me the "GGCTX" groups.
Thank tou for your comment.
I already found a solution like yours, but I would like to have a script that only gives me the "GGCTX" groups.
ASKER
This is the script that gives me a screen full of errors and garble:
Import-CSV "C:\Temp\Test\Test.csv" -Delimiter ';' | ForEach-Object {
$user = $_.samaccountname
$dn = (Get-ADUser $user).DistinguishedName
$GrpArr = @()
$Groups = get-adgroup -filter {name -like "GGCTX*"} -searchbase "dc=company,dc=local" | select $dn
foreach ($group in $groups)
{
$GrpArr += $group
$members = get-adgroupmember $group | select $dn
foreach ($member in $members)
{
$memprops = get-aduser $member -properties company
$comp = $memprops.company
$grpArr += "$member,$comp"
}
}
}
$grpArr | export-csv C:\Temp\Test\Groups.csv -NoTypeInformation
Import-CSV "C:\Temp\Test\Test.csv" -Delimiter ';' | ForEach-Object {
$user = $_.samaccountname
$dn = (Get-ADUser $user).DistinguishedName
$GrpArr = @()
$Groups = get-adgroup -filter {name -like "GGCTX*"} -searchbase "dc=company,dc=local" | select $dn
foreach ($group in $groups)
{
$GrpArr += $group
$members = get-adgroupmember $group | select $dn
foreach ($member in $members)
{
$memprops = get-aduser $member -properties company
$comp = $memprops.company
$grpArr += "$member,$comp"
}
}
}
$grpArr | export-csv C:\Temp\Test\Groups.csv -NoTypeInformation
How about the following? Not sure exactly what information you want returned.
Get-ADUser -filter * -Properties memberof | Where {$_.memberof -match "GGCTX"} | ForEach `
{
$samaccountname = $_.samaccountname
Get-ADPrincipalGroupMembership $_.samaccountname |
Where {$_.name -like "GGCTX*"} |
Select @{n="samaccountname";e={$samaccountname}},@{n="GroupName";e={$_.name}}
} | Export-CSV groups.csv -notype
ASKER
Hi footech,
You really helped me on this one !
I made a little adjustment, so now it grabs the sAMAccountname from a csv:
You really helped me on this one !
I made a little adjustment, so now it grabs the sAMAccountname from a csv:
Import-CSV "C:\Temp\Users.csv" -Delimiter ',' | ForEach-Object {
Get-ADUser -filter * -Properties memberof | Where {$_.memberof -match "GGCTX"} | ForEach `
{
$samaccountname = $_.samaccountname
Get-ADPrincipalGroupMembership $_.samaccountname |
Where {$_.name -like "GGCTX*"} |
Select @{n="samaccountname";e={$samaccountname}},@{n="GroupName";e={$_.name}}
} | Export-CSV C:\Temp\groups.csv -notype
}
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for Loyall's comment #a39657279
Assisted answer: 500 points for footech's comment #a39656940
for the following reason:
Footech gave me the handle to adjust the script exactly to my wishes.
Accepted answer: 0 points for Loyall's comment #a39657279
Assisted answer: 500 points for footech's comment #a39656940
for the following reason:
Footech gave me the handle to adjust the script exactly to my wishes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Footech delivered a script that really works ! ;-)
ASKER
Footech,
As I already wrote in my question, i'm a lousy scripter. ;-)
Thanks a lot for helping me !
Loyall
As I already wrote in my question, i'm a lousy scripter. ;-)
Thanks a lot for helping me !
Loyall
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Users,DC=domain,DC=loc
write-host "User:" $_.Name -foreground green
Get-ADPrincipalGroupMember
write-host "Member Of:" $_.name
}
}
the code will simply query all users in specified OU and return there group membership
I tried to chnage \ redirect output to csv format unsuccessfully.
If you could change the code so that you can get output in .csv format
later on you can filter excel file on the basis of group mentioned in question.