shadi1989
shadi1989

Reputation: 9

Powershell - query all users who only belong to domain users

I would like an active directory query to list all users who only belong to "Domain Users" and no other groups.

I already tried the following query, but it showed all users with all groups they belong to:

Import-Module Activedirectory
Get-ADUser -Filter * -Properties DisplayName,memberof | % {
  New-Object PSObject -Property @{
    UserName = $_.DisplayName
    Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","
    }
} | Select UserName,Groups | Export-Csv C:\temp\report.csv -NTI

Upvotes: 1

Views: 670

Answers (2)

Frode F.
Frode F.

Reputation: 54981

Search for an empty memberof-property while PrimaryGroup is "Domain Users". No need to list the groups if you expect nothing.

Get-ADUser -Filter "samaccountname -eq 'froflatest-sshf'" -Properties Memberof, PrimaryGroup, DisplayName, Description |
Where-Object { -not ($_.memberof) -and $_.PrimaryGroup -match 'Domain Users' } |
Select-Object SamAccountName, DisplayName, Description |
Export-CSV -Path "c:\report.csv" -NoTypeInformation

Upvotes: 1

shadi1989
shadi1989

Reputation: 9

Import-Module Activedirectory
Get-ADUser -Filter "*" -Properties sAMAccountName,Description, Memberof, PrimaryGroup |
Where-Object { -not ($_.memberof) -and $_.PrimaryGroup -match 'Domain Users' } | Select sAMAccountName,Description | Export-Csv C:\temp\report.csv -NTI

Upvotes: 0

Related Questions