Last active
November 30, 2023 09:19
-
-
Save Apoc70/5c605926303c7e0c86fa8ae261d5d487 to your computer and use it in GitHub Desktop.
Export Exchange security group members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script exports all Exchange security groups that have members | |
# Empty groups are not included in the CSV output. | |
# Your domain DN | |
$Domain = 'DC=varunagroup,DC=de' | |
# Preferred domain controller FQDN | |
$DomainControllerFQDN = 'testms01.varunagroup.de' | |
# Fetch Exchange security groups | |
$ExchangeSecurityGroups = Get-ADGroup -SearchBase ('OU=Microsoft Exchange Security Groups,{0}' -f $Domain ) -Server ('{0}:3268' -f $DomainControllerFQDN) -Filter * | |
Write-Host ('{0} Exchange Security Groups found' -f ($ExchangeSecurityGroups | Measure-Object).Count ) | |
# Helper variables | |
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path | |
$Result=@() | |
# Fetch members for each Exchange security group | |
foreach($Group in $ExchangeSecurityGroups) { | |
Write-Host $Group -ForegroundColor Gray | |
$GroupMembers = 0 | |
$GroupMembers = Get-ADGroupMember $Group.DistinguishedName -Recursive -Server $DomainControllerFQDN | |
foreach($GroupMember in $GroupMembers) { | |
Write-Host $GroupMember -ForegroundColor Green | |
$Object = New-Object System.Object | select GroupName, UserName, UserDN | |
$Object.GroupName = $Group.Name | |
$Object.UserName = $GroupMember.Name | |
$Object.UserDN = $GroupMember.DistinguishedName | |
$Result += $Object | |
} | |
} | |
if(($Result | Measure-Object).Count -gt 0) { | |
$Result | Export-Csv -Path ('{0}\ExchangeSecurityGroupMembers.csv' -f $ScriptDir) -Encoding UTF8 -Delimiter ';' -NoTypeInformation -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment