Jim,

THANKS!  I'll give both a try but user and group name is what I am looking for.

Thanks,
Ken ...

From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On 
Behalf Of Jim Truher
Sent: Friday, October 17, 2014 3:09 PM
To: powershell@lists.myitforum.com
Subject: [powershell] RE: Is user a member of security groups A, B or C


I'm not sure if you want to just capture the name of the user, or the group as 
well

Get-Content "C:\Temp\UserIDs2.txt" | ? {
    Get-ADPrincipalGroupMembership $_ | ?{
        $_.Name -match "group1|group2|group3"
        }
    } | out-file c:\temp\UserIdGroupMembership2.txt

This gets the content of the UserIDs, and then gets the group membership. Where 
the group name matches your desired string (in this case either group1, group2, 
or group3) it will output that username - not the group, just the username) to 
your output file.

However, if you need the user and the group, then you need to do something 
quite a bit different, and outputting as a CSV file might be better (since you 
need both the user and the group)

Get-Content "C:\Temp\UserIDs2.txt" | ForEach {
    $user = $_
    Get-ADPrincipalGroupMembership $user | ?{
        $_.Name -match "group1|group2|group3"
        } | %{
            [pscustomobject]@{ User = $user; Group = $_.name }
        }
    } | export-csv c:\temp\UserIdGroupMembership2.csv

I haven't actually run these, so I might have missed something, but this might 
be the way to go

From: listsad...@lists.myitforum.com<mailto:listsad...@lists.myitforum.com> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Lutz, Ken
Sent: Friday, October 17, 2014 2:12 PM
To: 'powershell@lists.myITforum.com'
Subject: [powershell] Is user a member of security groups A, B or C

I'm still trying to learn PowerShell and this is giving me fits.

I can get a list of all the groups that a user is a member of but I'm having 
trouble getting it to only return a select list of groups.

I have a txt file with a list of user IDs.  I would like to find out all the 
users that belong to security groups A, B or C.

Here is what I have to process the list of users and return all the groups that 
they are members off.  Now I want to find out is these users are a member of 
only 3 or 4 different groups.  I tried to put a Where-Object after the last 
ForEach and before the Add-Member items, but that didn't work.

$out = @()

Get-Content "C:\Temp\UserIDs2.txt" | ForEach {

$username = $_

$groups = Get-ADPrincipalGroupMembership $username

ForEach ( $group in $groups ) {

  $obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name UserName -Value $username
$obj | Add-Member -MemberType NoteProperty -Name GroupName -Value $group.name
# $obj | Add-Member -MemberType NoteProperty -Name Description -Value 
$group.description

$out += $obj

 }
}

$out | Out-file "C:\Temp\UserIDsGroupMembership2.txt"


Ken Lutz
Senior Systems Administrator
Information Systems Department
Spokane County
815 N. Jefferson
Spokane, Washington  99260
[cid:image001.png@01CFEA1E.841A5370]



================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to