Public/Get-GroupMembership.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
Function Get-GroupMembership
{
    <#
        .SYNOPSIS
            Lists group membership for all Active Directory users in a domain.
 
        .DESCRIPTION
            Lists group membership for all Active Directory users in a domain.
            This will enumerate the group membership for all users and
            either output to console or export to CSV.
 
        .PARAMETER Export
            If this switch is enabled, output will be directed to a CSV file.
 
        .EXAMPLE
            Get-GroupMembership -Export
         
        .EXAMPLE
            Get-GroupMembership
             
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$False,Position=0,HelpMessage="If used this will export to file instead of console.")]
        [Switch]
        $Export
    )
    
    $Output = @()
    $ADUsers = Get-ADUser -Filter *

    ForEach($ADUser in $ADUsers)
    {
        Try
        {
            $ADUserGroup = Get-ADPrincipalGroupMembership -Identity $ADUser
            $Properties = [Ordered]@{'Username'=$ADUser.Name;'GroupMembership'=$ADUserGroup.Name}
            $Output += New-Object -TypeName PSObject -Property $Properties
        }
        Catch
        {
            Write-Error "Failed obtaining group membership for user $($ADUser.Name)"
        }
    }

    If($Export)
    {
        $Output | Convert-OutputforCSV | Export-CSV UserGroupMembership.csv -NoTypeInformation
    }
    Else
    {
        $Output | Write-Output | Out-GridView
    } 
}