Delete folder in Exchange Online from all mailboxes

Lance Matthysen 21 Reputation points

Hi All,

Need your help,

We need to find a way to delete a folder from users' mailboxes in exchange online, we found the following article works great in our test environment, but due to the changes to authentication and basic authentication has been disabled, this script fails as it uses basic authentication, is there anyone who is able to advise on how this script can be changed or provide a solution. or what am I missing as the error is Error: The request failed. The remote server returned an error: (401) Unauthorized. permission is in place.

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,321 questions
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.
522 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,435 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kael Yao-MSFT 37,586 Reputation points Microsoft Vendor

    Hi @Lance Matthysen

    Since this question is related to Exchange development, I have added the tag "office-exchange-server-dev" to it.
    Thanks for your understanding.

    I tested the script in my lab and it works fine for me.
    Would it be possible to temporarily enable basic auth for this script to work?

    You may use this self-service tool to enable it.
    If you have Azure Security Defaults enabled, you may need to first disable it.
    And if the manage account has MFA enabled, please also disable it.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

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

    To change that script to use oAuth you just need to change the line

    $service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList (Get-Credential)  


    $service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.OAuthCredentials($accessToken)  

    the $accessToken variable needs to contain the oAuth Access Token which you will need some other code to acquire, there are a bunch of different approaches you could take the easiest maybe use the MSAL module from and you also need an application registration like

    then you can do something like this for a Delegate Token

    $token = Get-MsalToken -ClientId yourclientid -Scopes "" -RedirectUri ""  

    or for an AppToken

    $clientSecret = (ConvertTo-SecureString yourClientSecret -AsPlainText -Force )  
    $token = Get-MsalToken -clientID $clientID -clientSecret $clientSecret -tenantID $tenantID -Scopes ""  

    then use

    $accessToken = $token.AccessToken

    1 person found this answer helpful.
    0 comments No comments