Assistance at your fingertips...

Get ALL A User’s Groups

This will get every group a user is in, including the Primary..

‘Get-PromptedUserGroups.ps1’
‘That is ALL the user groups, including the Primary’
$day = Get-Date -Format “yyyyMMdd”
rm -force “D:\\powershell\\Reports\\$day-PromptedUsersGroupReport.csv”
Set-location D:\\PowerShell
Set-ExecutionPolicy Bypass CurrentUser -Force
Get-ExecutionPolicy CurrentUser
#Make sure the TLS goes on EVERY script!!!
# First, ensure TLS 1.2 for PowerShell gallery access.
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$user = (Read-Host -Prompt “What user do you want to check ALL groups for?”)
Get-ADUser $user -Properties SamAccountName, DisplayName, MemberOf, SID, primaryGroupID | ForEach-Object {
$userGroups = $_.memberof | Get-ADGroup | Select -ExpandProperty Name
$primaryGroup = Get-ADGroup -LDAPFilter (“(objectSID=” + $_.SID.Value.Substring(0,$_.SID.Value.LastIndexOf(“-“)) + “-” + $_.primaryGroupID + “)”)
$userGroups += $primaryGroup.Name
New-Object PSObject -Property @{
UserName = $_.DisplayName
oSamAccountname= $_.SamAccountname
UserSID = $_.SID
Groups = $userGroups -join “,”
}
} | Select Groups -ExpandProperty Groups 1

1 Use this for the last line to file export with other properties:
} | Select oSamAccountname,UserName,UserSID,Groups | Export-Csv -Path "D:\powershell\Reports\$day-AllUsersGroupsReport-withPrimary.csv" -NoTypeInformation -append

Posted

in

by