Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List group memberships for AD users

Tags:

powershell

Using the following Powershell snippet I get the names of the group memberships for the current user:

$groups = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
foreach($i in $groups){
$i.Translate([System.Security.Principal.NTAccount]).value
}

How can I modify this such I can supply the user account name as parameter?

Thanks,

Uwe

like image 523
Uwe Ziegenhagen Avatar asked Dec 22 '22 06:12

Uwe Ziegenhagen


1 Answers

If you have access to the ActiveDirectory module, I'd suggest you use Get-ADUser. In case you can't use that module, you could use the System.DirectoryServices.AccountManagement assembly:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$username = read-host -prompt "Enter a username"
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct, $username)
$groups = $user.GetGroups()
foreach($i in $groups){
  $i.SamAccountName
}
like image 199
jon Z Avatar answered Jan 15 '23 19:01

jon Z