Jump to content

Importing staff from the Universal security group to notepad or Excel


Recommended Posts

Posted

Hi

 

I have an ALL Staff Universal security group, which we use for sending email for ALL Staff.

 

Within this ALL Staff Universal security group, there are other departmental mail groups added as well. I would like to have the list of all users who are member of this ALL Staff Universal security group.

 

Is there a way of exporting all the members to a notepad or excel.

 

Any help will be great.

Thanks

Posted

Powershell is the way to go. To expand on @DaveTheTech's post:

Get-ADGroupMember 'Group Name' -Recursive | Select-Object sAMAccountName | Export-Csv C:\Temp\GroupMembers.csv -NoClobber -NoTypeInformation

Posted (edited)

So you may well not get the information that you are expecting by using the above command and this is drawn out in the documentation for the cmdlet https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-adgroupmember?view=winserver2012r2-ps (this will depend on what the hierarchy of the group looks like)

 

Get-ADGroupMember -Recurse

GroupEx1.PNG

 

Function Get-NestedGroupMembers

GroupEx2.PNG

 

As you can see there is quite a difference between the two results

 

Code for the function follows:-

 

function Get-NestedGroupMembers { param ( [Parameter(ValuefromPipeline=$true,mandatory=$true)][string] $GroupName, [int] $nesting = -1, [int]$circular = $null, [switch]$indent )     function indent      {     Param($list)         foreach($line in $list)         {         $space = $null                      for ($i=0;$i -lt $line.nesting;$i++)             {             $space += "    "             }             $line.name = "$space" + "$($line.name)"        }       return $List     }      $modules = get-module | select -expand name    if ($modules -contains "ActiveDirectory")     {         $table = $null         $nestedmembers = $null         $adgroupname = $null             $nesting++           $ADGroupname = get-adgroup $groupname -properties memberof,members         $memberof = $adgroupname | select -expand memberof         write-verbose "Checking group: $($adgroupname.name)"         if ($adgroupname)         {              if ($circular)             {                 $nestedMembers = Get-ADGroupMember -Identity $GroupName -recursive                 $circular = $null             }             else             {                 $nestedMembers = Get-ADGroupMember -Identity $GroupName | sort objectclass -Descending                if (!($nestedmembers))                {                    $unknown = $ADGroupname | select -expand members                    if ($unknown)                    {                        $nestedmembers=@()                        foreach ($member in $unknown)                        {                        $nestedmembers += get-adobject $member                        }                    }                }            }              foreach ($nestedmember in $nestedmembers)             {                 $Props = @{Type=$nestedmember.objectclass;Name=$nestedmember.name;DisplayName="";ParentGroup=$ADgroupname.name;Enabled="";Nesting=$nesting;DN=$nestedmember.distinguishedname;Email=$nestedmember.mail;Department=$nestedmember.department;Comment=""}                                  if ($nestedmember.objectclass -eq "user")                 {                     $nestedADMember = get-aduser $nestedmember -properties enabled,displayname,mail,department                    $table = new-object psobject -property $props                     $table.enabled = $nestedadmember.enabled                    $table.name = $nestedadmember.samaccountname                    $table.displayname = $nestedadmember.displayname                    $table.email = $nestedadmember.mail                    $table.department = $nestedadmember.department                    if ($indent)                     {                     indent $table | select @{N="Name";E={"$($_.name)  ($($_.displayname))"}}                    }                     else                     {                     $table | select type,name,displayname,parentgroup,nesting,enabled,dn,email,department,comment                     }                 }                 elseif ($nestedmember.objectclass -eq "group")                 {                      $table = new-object psobject -Property $props                                          if ($memberof -contains $nestedmember.distinguishedname)                     {                         $table.comment ="Circular membership"                         $circular = 1                     }                     if ($indent)                     {                     indent $table | select name,comment | %{                                                                                  if ($_.comment -ne "")                                         {                                         [console]::foregroundcolor = "red"                                         write-output "$($_.name) (Circular Membership)"                                         [console]::ResetColor()                                         }                                         else                                         {                                         [console]::foregroundcolor = "yellow"                                         write-output "$($_.name)"                                         [console]::ResetColor()                                         }                    }                                  }                    else                     {                     $table | select type,name,displayname,parentgroup,nesting,enabled,dn,mail,department,comment                     }                     if ($indent)                     {                        Get-NestedGroupMembers -GroupName $nestedmember.distinguishedName -nesting $nesting -circular $circular -indent                     }                     else                      {                        Get-NestedGroupMembers -GroupName $nestedmember.distinguishedName -nesting $nesting -circular $circular                     }                                                      }                 else                 {                                         if ($nestedmember)                    {                        $table = new-object psobject -property $props                        if ($indent)                         {                            indent $table | select name                         }                         else                         {                         $table | select type,name,displayname,parentgroup,nesting,enabled,dn,email,department,comment                            }                      }                }                           }          }     }     else {Write-Warning "Active Directory module is not loaded"}        }

 

Sorry stripped all the line breaks out will sort it at some point...

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...