Forum Discussion
How to list the disabled accounts and in which groups they are.
- Apr 19, 2023
The Approach from elieelkarkafi works fine.
$DisabledUsers = Get-ADUser -Filter {(Enabled -eq $false)} -Properties MemberOf | Select-Object Name, MemberOf
Foreach ($User in $DisabledUsers)
{
Write-Host "Username: $($User.Name)"
Foreach ($Group in $user.MemberOf)
{
Write-Host "Group: $Group"
}
}Regards
Andres
ADumith please try the below script
# Connect to Azure AD
Connect-AzureAD
# Get all disabled user accounts
$disabledUsers = Get-AzureADUser -Filter "AccountEnabled eq false"
# Loop through each disabled user account
foreach ($user in $disabledUsers) {
# Get the user's group membership
$groupMembership = Get-AzureADUserMembership -ObjectId $user.ObjectId
$groupNames = $groupMembership | ForEach-Object { $_.DisplayName }
# Print the user's name and group membership
Write-Host "User: $($user.DisplayName)"
Write-Host "Groups: $($groupNames -join ', ')"
Write-Host ""
}
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily.
- ADumithApr 19, 2023Iron Contributor
Hello elieelkarkafi,
Sorry I forgot to mention that my AD is on-prem, so I wonder this script won't work.There is a way to pull out only the users that are in any group? because I just realize that the script will provide all the users and I'm looking that thought they are not enabled they are still in some group.
Thank you in advance,- Apr 19, 2023
ADumith please use the below script , i added to it to export to csv file
Get-ADUser -Filter {(Enabled -eq $false)} -Properties MemberOf | Select-Object Name, MemberOf | Export-CSV -Path "C:\DisabledUsers.csv" -NoTypeInformation
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily.
- ADumithApr 19, 2023Iron ContributorHello,
I think something is wrong because all the user has the same Group which is Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Any ideas?
Thank you in advance,