-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserGroupMembership.ps1
62 lines (58 loc) · 1.82 KB
/
UserGroupMembership.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#
.SYNOPSIS
Simple script to produce a listing of user's group memberships
.DESCRIPTION
Script will create a simple listing of a user's group memberships.
Output is in object format so you can use other Powershell cmdlet's
with the output, such as Export-CSV, Out-File, ConvertTo-HTML, etc.
Groups are presented using the friendly name, and are sorted
alphabetically.
.PARAMETER User
Name of the user you want to list
.INPUTS
Pipeline
Get-ADUser
.OUTPUTS
PSObject User Name
Group Name
.EXAMPLE
.\Get-UserGroupMembership.ps1 -User thesurlyadmin
List all of the groups for "thesurlyadmin"
.NOTES
Author: Martin Pugh
Twitter: @thesurlyadm1n
Spiceworks: Martin9700
Blog: www.thesurlyadmin.com
Changelog:
1.0 Initial Release
.LINK
http://community.spiceworks.com/scripts/show/1872-get-user-group-memberships
#>
<#
Script will create a simple listing of a user's group memberships.
Output is in object format so you can use other Powershell cmdlet's with the output, such as Export-CSV, Out-File, ConvertTo-HTML, etc.
#>
Param (
[Parameter(Mandatory=$true,ValueFromPipeLine=$true)]
[Alias("ID","Users","Name")]
[string[]]$User
)
Begin {
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?"; Break }
}
Process {
ForEach ($U in $User)
{ $UN = Get-ADUser $U -Properties MemberOf
$Groups = ForEach ($Group in ($UN.MemberOf))
{ (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{ New-Object PSObject -Property @{
Name = $UN.Name
Group = $Group
}
}
}
}