You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell获取邮箱所有文件夹中的邮箱地址

实现遍历Outlook所有文件夹的PowerShell函数

修改需求

原Get-OutlookInBox函数仅能获取默认收件箱的邮件信息,现在需要修改为遍历邮箱所有文件夹(包括子文件夹),并提取邮件的Subject、ReceivedTime、Importance、SenderName、SenderEmailAddress字段,同时新增FolderPath字段标记邮件所在文件夹路径。

修改后的完整函数

Function Get-OutlookAllFolders 
{ 
    <# 
    .Synopsis 
    遍历Outlook默认配置文件的所有文件夹,返回邮件信息
    .Description 
    该函数通过Outlook互操作程序集连接到默认MAPI命名空间,递归遍历所有邮件文件夹(包括子文件夹),
    为每个邮件项创建包含Subject、ReceivedTime、Importance、SenderName、SenderEmailAddress以及FolderPath的自定义对象。
    注意:根据邮箱大小,遍历过程可能需要数分钟,建议将结果存入变量后进行离线分析。
    .Example 
    Get-OutlookAllFolders | Where-Object { $_.ReceivedTime -gt [datetime]"2024/01/01" } | Sort-Object Importance -Descending
    显示2024年1月1日之后收到的所有邮件,按重要性降序排列
    .Example 
    $allEmails = Get-OutlookAllFolders
    $allEmails | Group-Object SenderEmailAddress | Sort-Object Count -Descending | Select-Object Count, Name -First 10
    将所有邮件存入变量后,统计发件人邮箱的出现次数,显示Top10发件人
    .Notes 
    NAME:  Get-OutlookAllFolders
    BASED ON: 原Get-OutlookInbox函数(作者ed wilson, msft)
    LASTEDIT: 2024/05/20
    KEYWORDS: Microsoft Outlook, PowerShell, 邮件遍历
    #Requires -Version 2.0 
    #> 

    Add-type -assembly "Microsoft.Office.Interop.Outlook" | Out-Null 
    $olItemClass = "Microsoft.Office.Interop.Outlook.OlObjectClass" -as [type]
    $outlook = New-Object -ComObject Outlook.Application
    $namespace = $outlook.GetNameSpace("MAPI")

    # 递归遍历所有文件夹的内部函数
    function Get-AllOutlookFoldersRecursive {
        param(
            [Parameter(Mandatory=$true)]
            $Folder,
            [string]$CurrentPath
        )
        # 构建当前文件夹路径
        $folderPath = if ([string]::IsNullOrEmpty($CurrentPath)) { $Folder.Name } else { "$CurrentPath\$($Folder.Name)" }
        
        # 提取当前文件夹中的邮件项(仅筛选邮件类)
        $Folder.Items | Where-Object { $_.Class -eq $olItemClass::olMail } | 
            Select-Object -Property Subject, ReceivedTime, Importance, SenderName, SenderEmailAddress,
                @{Name='FolderPath'; Expression={ $folderPath }}
        
        # 递归处理子文件夹
        foreach ($subFolder in $Folder.Folders) {
            Get-AllOutlookFoldersRecursive -Folder $subFolder -CurrentPath $folderPath
        }
    }

    # 遍历所有顶级邮箱文件夹(覆盖多账户场景)
    foreach ($topFolder in $namespace.Folders) {
        Get-AllOutlookFoldersRecursive -Folder $topFolder -CurrentPath ""
    }

    # 释放Outlook COM对象,避免进程残留
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
} #end function Get-OutlookAllFolders

关键修改说明

  • 递归遍历逻辑:新增内部函数Get-AllOutlookFoldersRecursive,负责遍历当前文件夹及其所有子文件夹,解决原函数仅能访问单个文件夹的问题。
  • 文件夹路径标记:通过计算FolderPath字段,明确标记每封邮件所在的文件夹层级,方便后续筛选分析。
  • 邮件类型筛选:添加Where-Object { $_.Class -eq $olItemClass::olMail },确保仅提取邮件项,排除日历、任务等其他类型的文件夹内容。
  • COM对象释放:添加COM对象释放和垃圾回收逻辑,避免Outlook进程在脚本结束后残留。
  • 函数注释更新:同步更新了函数的描述、示例等注释内容,适配新功能。

内容的提问来源于stack exchange,提问作者panes-rubrics

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 17:30:21