当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 导出电子邮件地址

作者:精品下载站 日期:2024-12-14 05:53:51 浏览:12 分类:玩电脑

如何使用 PowerShell 导出电子邮件地址


每个邮箱或收件人可以有多个电子邮件地址。您可以搜索并获取组织中单个或所有用户的所有电子邮件地址。这些是主要 SMTP 地址和辅助 smtp 别名。在本文中,您将了解如何使用 PowerShell 将电子邮件地址导出到 CSV 文件。

管理 Exchange Online 中的电子邮件地址

要管理 Microsoft 365 组织中的电子邮件地址,请使用以下文章:

  • 使用 PowerShell 添加电子邮件地址
  • 使用 PowerShell 删除电子邮件地址
  • 使用 PowerShell 导出电子邮件地址(本文)

主要电子邮件地址与辅助电子邮件地址

您组织中的某些用户拥有多个电子邮件地址。您可以搜索并查找单个用户和所有用户的特定电子邮件地址。

每个用户都应该有一个主 SMTP 地址。辅助 smtp 电子邮件地址也称为别名。别名是人们可以用来向用户发送电子邮件的另一个电子邮件地址。还有代理电子邮件地址,它们是称为 SIP 或 x500 地址的附加地址。

下表显示了 SMTPsmtp 电子邮件地址之间的区别。

Only one address

零个、一个或多个地址

Primary email address

辅助电子邮件地址或别名

Written in uppercase letters (SMTP)

以小写字母书写 (smtp)

Use PrimarySmtpAddress in PowerShell to display only SMTP addresses.

在 PowerShell 中使用 EmailAddresses 显示所有电子邮件地址(SMTP、smtp、SIP、x500 地址)

The recipient can send and accept messages from the SMTP email address.

默认情况下,收件人只能接受发送到 smtp 电子邮件地址的电子邮件。除非您在 Microsoft 365 租户中启用从别名发送,否则收件人无法从别名地址发送。*

*如果您有 Exchange Online 邮箱,则只能从别名电子邮件地址发送,因为该功能不适用于 Exchange 本地邮箱。

连接到 Exchange Online PowerShell

若要运行当前文章中指定的 PowerShell 命令,您必须连接到 Exchange Online PowerShell。

Connect-ExchangeOnline

获取单个邮箱电子邮件地址

我们将使用 Get-Mailbox cmdlet 来显示有关电子邮件地址的信息。

要获取单个用户邮箱的所有电子邮件地址,请运行以下 PowerShell 命令。

Get-Mailbox "[email protected]" | Select-Object -ExpandProperty EmailAddresses

PowerShell 输出显示以下结果。

SMTP:[email protected]
SPO:SPO_a21d1f73-5b3e-47db-bccd-16559e7d5560@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd

获取主 SMTP 地址

运行以下 PowerShell 命令以显示特定 Exchange Online 收件人的主 SMTP 地址

Get-Mailbox "[email protected]" | Select-Object DisplayName, PrimarySmtpAddress

PowerShell 输出显示以下结果。

DisplayName   PrimarySmtpAddress
-----------   ------------------
Amanda Hansen [email protected]

要显示所有 Exchange Online 邮箱的主 SMTP 地址,请运行以下 PowerShell 命令。

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySmtpAddress

仅显示 smtp 电子邮件地址

使用 Get-Mailbox cmdlet,您无法指定电子邮件地址类型,例如代理电子邮件地址。因此,您需要添加哈希表,它是PowerShell命令中的一种特殊数组。

哈希表分为以下几部分创建:

  1. 使用 @ 字符创建哈希表,后跟大括号 { } 和名称。
  2. 使用=字符并定义数组名称
  3. 表达式部分用于创建经过 PowerShell 过滤的搜索查询。
  4. 使用 Where-Object PowerShell 语句和比较运算符 -cmatch 定义过滤器,仅区分大小写以获取 smtp 电子邮件地址。

运行 PowerShell 命令以仅列出单个邮箱的 smtp 电子邮件地址

Get-Mailbox "[email protected]" | Select-Object DisplayName, @{Name = "EmailAddresses"; Expression = { ($_.EmailAddresses | Where-Object { $_ -cmatch "smtp:*" } | ForEach-Object { $_ -replace 'smtp:' }) -join ';' } }

PowerShell 输出显示以下输出。

DisplayName  EmailAddresses
-----------  --------------
Brenda Smith [email protected];[email protected];[email protected]

要显示所有 Exchange Online 邮箱的 smtp 电子邮件地址,请运行以下 PowerShell 命令。

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, @{Name = "EmailAddresses"; Expression = { ($_.EmailAddresses | Where-Object { $_ -cmatch "smtp:*" } | ForEach-Object { $_ -replace 'smtp:' }) -join ';' } }

仅显示 SIP 地址

要仅显示所有邮箱的 SIP 地址,您需要将 Where-Object-like PowerShell 比较运算符结合使用。

运行以下 PowerShell 命令以显示所有用户的所有 SIP 地址。

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, @{Name = "SIP Email Address"; Expression = { $_.EmailAddresses | Where-Object { $_ -like "sip:*" } } }

PowerShell 输出显示以下结果。

DisplayName              SIP Email Address
-----------              -----------------
Adam Mackay              SIP:[email protected]
Amanda Hansen
Amanda Morgan            SIP:[email protected]
Andrea Baker             SIP:[email protected]
Anna Bell
Blake Martin             SIP:[email protected]
Brenda Smith             SIP:[email protected]
Brian Mill               SIP:[email protected]
Carl Hawk                SIP:[email protected]

仅显示 x500 地址

要仅查看所有邮箱的 x500 电子邮件地址,您需要将 Where-Object-cmatch PowerShell 比较运算符结合使用。

运行以下 PowerShell 命令以获取所有邮箱的所有 x500 地址的列表。

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, @{Name = "x500 Email Address"; Expression = { $_.EmailAddresses | Where-Object { $_ -cmatch "x500:*" } } }

搜索具有特定域名后缀的所有电子邮件地址

要搜索特定电子邮件地址,您可以使用 PowerShell 中的 Where-Object。为了过滤结果,我们将使用 -eq-like PowerShell 比较运算符。

PowerShell 语法将类似于以下使用 -eq 的示例。

Where-Object { $_.EmailAddresses -eq "smtp:[email protected]" }

另一个使用 -like 的 PowerShell 语法示例。

Where-Object { $_.EmailAddresses -like "*@a-d.site" }

在我们的示例中,我们希望获取域名后缀为 a-d.site 的所有电子邮件地址。

运行以下 PowerShell 命令以获取所有邮箱的具有特定域名后缀的所有主 SMTP

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySMTPAddress, @{Name = "Proxy Email Addresses"; Expression = { $_.EmailAddresses -cmatch "smtp" } } | Where-Object { ($_.PrimarySMTPAddress -like "*a-d.site*") }

PowerShell 输出显示用户所有电子邮件地址的列表。

DisplayName        PrimarySmtpAddress              Proxy Email Addresses
-----------        ------------------              ---------------------
Adam Mackay        [email protected]        {smtp:[email protected], smtp:[email protected]}
Amanda Hansen      [email protected]
Andrea Baker       [email protected]
Anna Bell          [email protected]          smtp:[email protected]
Blake Martin       [email protected]
Brenda Smith       [email protected]       {smtp:[email protected], smtp:[email protected], smtp:[email protected]}
Brian Mill         [email protected]
Carl Hawk          [email protected]          smtp:[email protected]
Carol Piper        [email protected]        {smtp:[email protected], smtp:[email protected]}
Catch All          [email protected]          smtp:[email protected]
Chris Lucas        [email protected]        smtp:[email protected]
Test SharedMailbox [email protected] smtp:[email protected]

要搜索所有电子邮件地址,我们将使用 Get-Recipient 来遍历所有启用邮件的对象。我们将添加带有特定域后缀的-Filter参数。

运行 PowerShell 命令示例以获取所有收件人的所有电子邮件地址。

Get-Recipient -ResultSize Unlimited -Filter { EmailAddresses -like "*@a-d.site" } | Format-Table DisplayName, EmailAddresses

PowerShell 输出结果。

DisplayName              EmailAddresses
-----------              --------------
Amanda Hansen            {SMTP:[email protected], SPO:SPO_a21d1f73-5b3e-47db-bccd-16559e7d5560@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd}
Chris Lucas              {SIP:[email protected], smtp:[email protected], SMTP:[email protected]}
Diana Baker              {SIP:[email protected], SMTP:[email protected], SPO:SPO_13fee752-5966-4b62-98e2-f27fe9e5f66d@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd}
Info Box                 {SIP:[email protected], SMTP:[email protected]}
Projector 8              {SIP:[email protected], SMTP:[email protected]}

要在 Exchange Online 中搜索软删除邮箱的电子邮件地址,请运行以下 PowerShell 命令。

Get-Mailbox -ResultSize Unlimited -SoftDeletedMailbox -Filter { EmailAddresses -like "*@a-d.site" } | Format-Table DisplayName, EmailAddresses

PowerShell输出结果如下所示。

DisplayName        EmailAddresses
-----------        --------------
Test SharedMailbox {DLTDNETID:10032003642DB69B, SMTP:[email protected], smtp:[email protected]}

运行以下 PowerShell 命令以获取所有通讯组电子邮件地址的列表。

Get-DistributionGroup -ResultSize Unlimited -Filter { EmailAddresses -like "*@a-d.site" } | Format-Table DisplayName, EmailAddresses

请参阅下面的 PowerShell 输出结果。

DisplayName          EmailAddresses
-----------          --------------
Finance UK mail list {smtp:[email protected], SMTP:[email protected]}
RoomGroup            {SMTP:[email protected]}
Sales Spain          {smtp:[email protected], SMTP:[email protected]}
Sales Spain          {smtp:[email protected], SMTP:[email protected]}
Sales USA            {smtp:[email protected], SMTP:[email protected]}
SalesUK              {smtp:[email protected], SMTP:[email protected]}

运行以下 PowerShell 命令以获取所有动态通讯组电子邮件地址。

Get-DynamicDistributionGroup -ResultSize Unlimited -Filter { EmailAddresses -like "*@a-d.site" } | Format-Table DisplayName, EmailAddresses

请参阅下面的 PowerShell 输出结果。

DisplayName    EmailAddresses
-----------    --------------
Dynamic        {SMTP:[email protected]}
Dynamic test 2 {SMTP:[email protected]}

运行以下 PowerShell 命令以获取所有统一组电子邮件地址。

Get-UnifiedGroup -ResultSize Unlimited -Filter { EmailAddresses -like "*@a-d.site" } | Format-Table DisplayName, EmailAddresses

PowerShell输出结果如下所示。

DisplayName        EmailAddresses
-----------        --------------
Sales Team         {SPO:SPO_079142cc-bb67-440e-b99a-0be94049c99a@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], SMTP:[email protected]}
Management Team    {SPO:SPO_05de02ce-e6eb-48dc-8020-2121678d9382@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], smtp:[email protected]}
IT                 {SPO:SPO_5c3acbd0-31e8-4788-b6be-d2cb0f7e3029@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], SMTP:[email protected]}
M365 Dynamic Group {SPO:SPO_839a2af8-6518-4712-ae12-8e9025c9bf2c@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], SMTP:[email protected]}
ZZgroup            {SPO:SPO_e82d151f-a843-4e9e-8474-c5b3304c0b41@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], SMTP:[email protected]}
Sales              {SPO:SPO_336a3658-0414-4c8c-8d9c-fa2b7b71d071@SPO_a2ff010e-0e03-4c56-8863-2ae7f07876dd, smtp:[email protected], SMTP:[email protected]}

将所有电子邮件地址导出到 CSV 文件

要将所有电子邮件地址导出到 CSV 文件,您需要在 (C:) 驱动器中创建一个 temp 文件夹。

  1. 第 2 行中指定CSV 文件路径
  2. 运行 PowerShell 脚本将所有电子邮件地址导出到 Out-GridViewCSV 文件
# CSV export file path
$CsvPath = "C:\temp\AllEmailAddresses.csv"

# Initialize an empty array to store recipient information
$recipientInfo = @()

# Retrieve recipient information
$recipients = Get-recipient -ResultSize Unlimited

# Iterate through each recipient
foreach ($recipient in $recipients) {
    $recipientData = [PSCustomObject]@{
        DisplayName               = $recipient.DisplayName
        'Primary Email Addresses' = ($recipient.EmailAddresses | Where-Object { $_ -cmatch "SMTP:" } | ForEach-Object { $_ -replace "SMTP:", "" }) -join ";"
        'Proxy Email Addresses'   = ($recipient.EmailAddresses | Where-Object { $_ -cmatch "smtp:" } | ForEach-Object { $_ -replace "smtp:", "" }) -join ";"
        'SIP Email Addresses'     = ($recipient.EmailAddresses | Where-Object { $_ -like "sip:*" } | ForEach-Object { $_ -replace "sip:", "" }) -join ";"
        'X500 Email Addresses'    = ($recipient.EmailAddresses | Where-Object { $_ -like "x500:*" } | ForEach-Object { $_ -replace "x500:", "" }) -join ";"
    }
    $recipientInfo += $recipientData
}

# Output the recipient information to Out-GridView
$recipientInfo | Out-GridView -Title "Microsoft 365 email addresses"

# Export the recipient information to a CSV file
$recipientInfo | Export-Csv $CsvPath -NoTypeInformation -Encoding utf8
  1. 显示 Out-GridView 结果

[玩转系统] 如何使用 PowerShell 导出电子邮件地址

  1. 使用 Microsoft Excel 打开 CSV 文件以查看结果

[玩转系统] 如何使用 PowerShell 导出电子邮件地址

就是这样!

了解更多:如何导出完全访问邮箱权限 »

结论

您学习了如何使用 PowerShell 搜索和导出电子邮件地址。 PowerShell 命令中可以使用不同的查询来检索所需的结果。另一种方法是导出所有电子邮件地址并在 Microsoft Excel 中过滤它们。

您喜欢这篇文章吗?您可能还喜欢如何在 PowerShell 中使用逻辑运算符。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯