Score:0

How can I get a list of all disabled domain users with group membership, one user per line, but omit results for users that have no membership?

in flag

For example, when our users are terminated, we need to remove all groups. I want to check for groups still connected to user, but not show the users where the groups are removed.

I have the following, which shows all disabled users, but can't figure out how to test for a null group. Any help would be appreciated.

Import-Module Activedirectory
Get-ADUser -Filter 'enabled -eq $false' -Properties DisplayName,memberof | % {
New-Object PSObject -Property @{
UserName = $_.DisplayName
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join 
","}
} | Select UserName,Groups
br flag
PLEASE read the Tour page for this site ... and use the code formatting instructions linked on that page. [*grin*]
Appleoddity avatar
ng flag
All users are a member of at least one group.
Score:1
cn flag

Add a where check before the select:

| Where-Object {$_.Groups -ne $null} 


Import-Module Activedirectory
Get-ADUser -Filter 'enabled -eq $false' -Properties DisplayName,memberof | % {
    New-Object PSObject -Property @{
    UserName = $_.Name
    Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","}
} | Where-Object {$_.Groups -ne $null} | Select UserName,Groups 
in flag
Thanks Greg! This runs, however I'm still seeing blank groups in the output. Do you know a better way to filter this? @GregAskew. Thanks a million btw
in flag
UserName Groups -------- ------ John Lennon Domain Admins George Harrison George Martin Enterprise Admins Brian Epstein
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.