genesis Posted November 25, 2019 Posted November 25, 2019 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
DaveTheTech Posted November 25, 2019 Posted November 25, 2019 Powershell and use Get-ADGroupMember with the recursive switch Then pipe that to export-csv
CHiLL Posted November 25, 2019 Posted November 25, 2019 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
HPlum78 Posted November 25, 2019 Posted November 25, 2019 (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 Function Get-NestedGroupMembers 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 November 25, 2019 by HPlum78
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now