none
How to export all AD groups and show only members that are enabled

    Question

  • Hello,

    I have grabbed someone else's script from another site and have been trying to alter it but to no avail.  Basically I want to export into a .csv file all of the AD security groups and any associated members that are enabled. 

    The following script exports security groups and all their members, however I'm just chasing the security groups and only enabled members.  

    TIA

    Import-Module ActiveDirectory

    $Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -ExpandProperty name)

    $Table = @()

    $Record = @{
      "Group Name" = ""
      "Name" = ""
      "Username" = ""
    }


    Foreach ($Group in $Groups) {

      $Arrayofmembers = Get-ADGroupMember -identity $Group | select name,samaccountname

      foreach ($Member in $Arrayofmembers) {
        $Record."Group Name" = $Group
        $Record."Name" = $Member.name "Enabled -ne $false'
        $Record."UserName" = $Member.samaccountname
        $objRecord = New-Object PSObject -property $Record
        $Table += $objrecord

      }
    }

    $Table | export-csv "C:\Temp\GroupReport.csv" -NTI



    • Edited by sumsysadmin Thursday, July 12, 2018 5:34 AM
    Thursday, July 12, 2018 5:28 AM

Answers

  • Import-Module ActiveDirectory
    
    $Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -ExpandProperty name)
    
    $Table = @()
    
    $Record = @{
      "Group Name" = ""
      "Name" = ""
      "Username" = ""
    }
    
    
    Foreach ($Group in $Groups) {
    
      $Arrayofmembers = Get-ADGroupMember -identity $Group | select name,samaccountname
    
      foreach ($Member in $Arrayofmembers) {
        If ((Get-ADUser -Identity $Member).Enabled -eq $true){
            $Record."Group Name" = $Group
            $Record."Name" = $Member.name 
            $Record."UserName" = $Member.samaccountname
            $objRecord = New-Object PSObject -property $Record
            $Table += $objrecord
        }
      }
    }
    
    $Table | export-csv "C:\Temp\GroupReport.csv" -NoTypeInformation
    This is what I added: 
    If ((Get-ADUser -Identity $Member).Enabled -eq $true)
    • Marked as answer by sumsysadmin Monday, July 16, 2018 5:16 AM
    Thursday, July 12, 2018 9:30 AM
  • Thanks heaps for your help.  I was getting some link errors and needed to modify slightly the extra line you suggested but otherwise it was exactly what I was after.  

    I modified it to:

    If ((Get-ADUser -Identity $Member.samaccountname).Enabled -eq $true){

    • Marked as answer by sumsysadmin Monday, July 16, 2018 5:16 AM
    Monday, July 16, 2018 5:16 AM

All replies