[玩转系统] 使用 PowerShell 将 Microsoft 365 组成员导出到 CSV
作者:精品下载站 日期:2024-12-14 06:31:49 浏览:15 分类:玩电脑
使用 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 -Force
Install-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.a-d.site/export-microsoft-365-group-members-to-csv-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.a-d.site
LinkedIn: linkedin.com/in/a-d
.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.ps1
4. 打开 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 组报告。
您喜欢这篇文章吗?您可能还喜欢阻止从共享邮箱登录。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag