Exclude disabled users

Crod 266 Reputation points
2020-11-20T14:18:01.447+00:00

I'm using the following but need to excluded disabled users please:

Get-ADGroupMember -server 'domain.com' -identity MyTestGrp | Select SamAccountName,name | Export-csv -path C:\temp\MyTestGrp.csv -NoTypeInformatio

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,522 questions
0 comments No comments
{count} votes

Accepted answer
  1. SChalakov 10,386 Reputation points MVP
    2020-11-20T16:32:18.423+00:00

    Hi @Crod ,

    here is the previous example with the export to CSV:

     $Users = Get-ADGroupMember -Identity MyTestGrp| ? {$_.objectclass -eq "user"}  
     $AllUsers = foreach ($ActiveUser in $Users)   
     { Get-ADUser -Identity $Activeuser | ? {$_.enabled -eq $true} | select Name, SamAccountName, UserPrincipalName, Enabled }   
     $AllUsers | Export-Csv -Path C:\Temp\USers.csv -NoTypeInformation -Encoding UTF8  
    

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)
    Regards,
    Stoyan

    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 109.1K Reputation points MVP
    2020-11-20T14:56:47.56+00:00

    Maybe this is helpful:

    $users = Get-ADGroupMember -Server 'domain.com' -Identity MyTestGrp| where {$_.objectclass -eq "user"}
    

    foreach ($activeUser in $users) { Get-ADUser -Identity $activeUser | where {$_.enabled -eq $true} | select SamAccountName, name}

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  2. SChalakov 10,386 Reputation points MVP
    2020-11-20T15:00:45.757+00:00

    Hi @Crod ,

    you are getting groups, so you will have to go through all group membersand filter users by the "disabled" flag. So it should look more like this:

    $Users = Get-ADGroupMember -Identity MyTestGrp | ? {$_.objectclass -eq "user"}  
    foreach ($ActiveUser in $Users) { Get-ADUser -Identity $Activeuser | ? {$_.enabled -eq $true} | select Name, SamAccountName, UserPrincipalName, Enabled }  
    

    Haven't tested it, but should do the job. Will update the thread as soon as I have tested it :)

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)
    Regards,
    Stoyan

    0 comments No comments

  3. Rich Matheisen 46,711 Reputation points
    2020-11-20T15:36:38.91+00:00

    Here's another take on a way to accomplish this. This is untested:

    Get-ADGroupMember -Server 'domain.com' -Identity MyTestGrp |
        ForEach-Object{
            Get-ADObject -Filter {distinguishedName -eq $_.distinguishedName -and objectClass -eq 'user' -and Enabled -eq $true} |
                Select-Object SamAccountName, Name
        }
    
    0 comments No comments

  4. Crod 266 Reputation points
    2020-11-20T15:38:45.917+00:00

    How can I export it?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.