使用 PowerShell 将 Microsoft 365 组成员导出到 CSV
组织中有很多组,我们想知道每个组中添加了哪些成员。我们可以手动浏览这些组并将它们写下来,但这需要很长时间。另一种方法是使用 PowerShell 来加速该过程。在本文中,您将了解如何使用 PowerShell 将 Microsoft 365 组成员导出到 CSV。
Microsoft 365 组类型
Microsoft 365 中有 4 种不同的组类型,我们希望导出所有这些组,包括其成员。
1.微软365
通过为团队提供群组电子邮件以及用于对话、文件和日历的共享工作区,允许团队进行协作。在 Outlook 中,这些称为组。
2.通讯组列表
为一群人创建一个电子邮件地址。
3.动态通讯组列表
向列表中的所有成员发送电子邮件。该群组的成员列表每 24 小时更新一次,基于您设置的过滤器和条件。
4.支持邮件的安全性
向群组的所有成员发送消息并授予对“OneDrive”、“SharePoint”和管理员角色等资源的访问权限
查找 Microsoft 365 组
若要查找 Microsoft 365 中的所有组,请按照以下步骤操作:
登录 Microsoft Exchange 管理中心
单击收件人 > 组
单击群组类型以列出群组
让我们看看下一步如何使用 PowerShell 导出 Microsoft 365 组成员。
使用 PowerShell 脚本获取 Microsoft 365 组成员
Export-M365GroupMembers.ps1 PowerShell 将获取所有 Microsoft 365 组类型(包括其成员),并将它们导出到 CSV 文件。
对于每个 Microsoft 365 组,它收集以下信息:
组ID
组显示名称
组类型
用户显示名称
用户主体名称
用户别名
用户类型
-
用户帐户已启用
注意:该脚本将导出所有 Microsoft 365 组类型。如果特定群组中没有群组成员,则会显示为N/A(不适用)。
1.安装Microsoft Graph PowerShell模块
在我们进一步继续并从所有组中获取 Microsoft 365 组成员之前,我们需要安装 Microsoft Graph PowerShell 模块。
以管理员身份启动 Windows PowerShell 并运行以下命令。
Install-Module Microsoft.Graph -ForceInstall-Module Microsoft.Graph.Beta -AllowClobber -Force重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
2. 准备导出 Microsoft 365 组成员 PowerShell 脚本
在(C:)驱动器上创建两个文件夹:
温度
脚本
下载 Export-M365GroupMembers.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 CSV 文件导出到 C:\temp 文件夹。
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-M365GroupMembers.ps1 并将其放置在 C:\scripts 文件夹中。
<#
.SYNOPSIS
Export-M365GroupMembers.ps1
.DESCRIPTION
Export Microsoft 365 Group Members to CSV file with PowerShell.
.LINK
www.alitajran.com/export-microsoft-365-group-members-to-csv-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 03/18/2024 - Initial version
#>
# CSV file path to export
$CsvPath = "C:\temp\M365GroupMembers.csv"
# Connect to Microsoft Graph with specified scopes
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All"
# Retrieve all groups
$groups = Get-MgGroup -All
# Get properties
$Properties = @(
'Id', 'DisplayName', 'UserPrincipalName', 'UserType', 'AccountEnabled'
)
# Initialize an array to store user information
$allUsers = @()
# Set up the progress bar parameters
$totalGroups = $groups.Count
$currentGroup = 0
# Iterate through each group and retrieve group members
foreach ($group in $groups) {
# Retrieve group members using the valid Group ID
$members = Get-MgGroupMember -GroupId $group.id -All
# Determine the group type
$groupType = if ($group.groupTypes -eq "Unified" -and $group.securityEnabled) { "Microsoft 365 (security-enabled)" }
elseif ($group.groupTypes -eq "Unified" -and !$group.securityEnabled) { "Microsoft 365" }
elseif (!($group.groupTypes -eq "Unified") -and $group.securityEnabled -and $group.mailEnabled) { "Mail-enabled security" }
elseif (!($group.groupTypes -eq "Unified") -and $group.securityEnabled) { "Security" }
elseif (!($group.groupTypes -eq "Unified") -and $group.mailEnabled) { "Distribution" }
else { "N/A" }
# If there are no members, create an object with empty values
if ($members.Count -eq 0) {
$Objects = [PSCustomObject][ordered]@{
GroupId = $group.Id
GroupDisplayName = $group.DisplayName
GroupType = $groupType
UserDisplayName = "N/A"
UserPrincipalName = "N/A"
UserAlias = "N/A"
UserType = "N/A"
UserAccountEnabled = "N/A"
}
$allUsers += $Objects
}
else {
# Iterate through each group member and retrieve user details
foreach ($member in $members) {
$user = Get-MgUser -UserId $member.Id -Property $Properties -ErrorAction SilentlyContinue | Select-Object $Properties
# Check if $user is not null before accessing properties
if ($user.Count -ne 0) {
# Extract the alias from the UserPrincipalName
$alias = $user.UserPrincipalName.Split("@")[0]
# Create an ordered custom object with properties in a specific order
$Objects = [PSCustomObject][ordered]@{
GroupId = $group.Id
GroupDisplayName = $group.DisplayName
GroupType = $groupType
UserDisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
UserAlias = $alias
UserType = $user.UserType
UserAccountEnabled = $user.AccountEnabled
}
# Add the ordered custom object to the array
$allUsers += $Objects
}
}
}
# Update the progress bar
$currentGroup++
$status = "{0:N0}" -f ($currentGroup / $totalGroups * 100)
$progressParams = @{
Activity = "Retrieving Group Members"
Status = "Processing group: $($group.DisplayName) - $currentGroup of $totalGroups : $status% completed"
PercentComplete = ($currentGroup / $totalGroups) * 100
}
Write-Progress @progressParams
}
# Complete the progress bar
Write-Progress -Activity "Retrieving Group Members" -Completed
# Export all user information to a CSV file
$allUsers | Sort-Object GroupDisplayName | Export-Csv $CsvPath -NoTypeInformation -Encoding utf8第 21 行:编辑 CSV 文件路径
3. 运行导出 Microsoft 365 组成员 PowerShell 脚本
获取所有 Microsoft 365 组(包括其成员)并将其导出到 CSV 文件。
运行以下命令以运行脚本 Export-M365GroupMembers.ps1。
c:\scripts\.\Export-M365GroupMembers.ps14. 打开 Microsoft 365 组成员报告
Export-M365GroupMembers.ps1 PowerShell 脚本将所有 Microsoft 365 组成员导出到 CSV 文件。
在路径 C:\temp 中找到文件 M365GroupMembers.csv。
使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。
Microsoft 365 组成员报告看起来很棒。
了解更多:导出 Microsoft 365 禁用用户报告 »
结论
您了解了如何使用 PowerShell 将 Microsoft 365 组成员导出到 CSV。虽然您可以从管理中心导出群组,但它不会获取群组成员。您必须使用 PowerShell 创建自定义 Microsoft 365 组报告。
您喜欢这篇文章吗?您可能还喜欢阻止从共享邮箱登录。不要忘记关注我们并分享这篇文章。


