Powershell script to get all group membership including nested groups

### Powershell script to get all users group membership including nested groups ###
# Load AD Module
Import-Module ActiveDirectory

# Define function for group lookups
function GetGroups ($object)
{
Get-ADPrincipalGroupMembership $object | ForEach `
{
$_
Get-ADPrincipalGroupMembership $_
}
}
# Set query to ask for username
$username = Read-Host -Prompt ‘Enter the username’

# Ask for a username to check & export results to CSV file
GetGroups $username | select name,GroupScope,GroupCategory -Unique | Export-CSV C:\Scripts\Reports\$username-All_Groups.csv