#Function: Get All Dynamic Groups Members from O365 Exchange
#Author: Junson Zeng
#Last Updated Date: July 8, 2021
echo '--------------------------------------------------------------------------------------------------------------'
echo 'If you are running this script for the first time,you need install the ExchangeOnlineManagement module first!'
echo '--------------------------------------------------------------------------------------------------------------'
Connect-ExchangeOnline
#Get dynamic groups name
$dynamicgroups = Get-DynamicDistributionGroup -SortBy DisplayName -ResultSize Unlimited
#Create a folder
$curPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$curDateTime = Get-Date -Format yyyyMMdd-HHmmss
New-Item $curPath\DynamicGroupMembers$curDateTime -type directory
echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
echo 'The script is in progress, please do not close the script...'
echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
#Get dynamic groups members and export to csv
ForEach ($dgm in $dynamicgroups){
$dgmembers = Get-Recipient -SortBy DisplayName -ResultSize unlimited -RecipientPreviewFilter $dgm.RecipientFilter -OrganizationalUnit $dgm.RecipientContainer | Select-Object DisplayName,WindowsLiveID,RecipientType | Export-Csv $curPath\DynamicGroupMembers$curDateTime\$dgm.csv -Encoding default
}
echo '------------------------------------------------------------'
echo 'The script has finished, please find the exported file.'
echo '------------------------------------------------------------'
pause
Guide to get dynamic group members
Author: Junson Zeng
Version: V2
Last Updated Date: July 8, 2021
先决条件:
需要一台安装好.NET Framework 4.0和ExchangeOnlineManagement模块的Windows电脑
ExchangeOnlineManagement模块安装方法如下:
以管理员方式打开powershell,运行以下命令:
Install-Module -Name ExchangeOnlineManagement
操作步骤:
1、将上面的代码保存成.ps1格式的powershell脚本,右键点击powershell脚本,选择使用powershell运行
2、如果是第一次运行脚本的电脑会出现此告警,这是因为windows默认禁止运行脚本,请选择Y以修改策略
3、登录具有O365管理员权限的账号
4、输入正确的账号密码后,脚本会自动在当前目录新建一个DynamicGroupMembers文件夹,并自动开始查找动态通讯组的成员和导出csv文件到此文件夹,整个过程所需的时间较长,请耐心等待
5、运行完成后,可以看到当前文件夹中总共有51个csv文件,代表目前总共有51个Dynamic Groups,每个Group的成员都在单独的csv文件中
6、可以使用Everything的高级搜索功能来快速获取某个用户所在的动态通讯组