Unable to find Outlook folders to remove from users Outlook

Alexis Crawford 201 Reputation points


The script is able to locate the specific Folder but is unable to delete it. I receive no error messages. Permission on Account in the production tenant is the same as the QA environment. I'm running this script with UPN credentials. This script in QA works as expected. I am unable to retrieve the UniqueFolderID whereas I can in the QA environment. Is there anything I can do to make it so the code can retrieve the UniqueFolderID?

All Outlook users are located in Exchange Online

#$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $_.Account)  
    $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot, $_.Account)     
    try {  
        $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderid)          
    catch {  
        Write-Host "Inbox cannot be found on $Account"  


Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
528 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,448 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points

    All Outlook users are located in Exchange Online

    What's your reason for using EWS to do this ? why not just use the Powershell graph sdk eg

    Get-MgUserMailFolder -MailFolderId MsgFolderRoot -UserId user@keyman

    its a lot simpler logic and supports all the different Auth scheme eg client_credentials etc, EWS is considered legacy and may go away at some point in the future the only reason to use it is if you have hybrid mailboxes or OnPrem.

    0 comments No comments