Delete folder in Exchange Online from all mailboxes

Lance Matthysen 21 Reputation points
2022-04-12T13:18:34.767+00:00

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 https://www.alitajran.com/delete-folder-in-exchange-online-from-all-mailboxes/ 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,175 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.
507 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,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kael Yao-MSFT 37,496 Reputation points Microsoft Vendor
    2022-04-13T05:38:23.93+00:00

    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.
    192497-42.png


    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
    2022-04-14T00:20:22.707+00:00

    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)  
    

    to

    $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 https://www.powershellgallery.com/packages/MSAL.PS/4.37.0.0 and you also need an application registration like https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth

    then you can do something like this for a Delegate Token

    $token = Get-MsalToken -ClientId yourclientid -Scopes "https://outlook.office365.com/EWS.AccessAsUser.All" -RedirectUri "https://login.microsoftonline.com/common/oauth2/nativeclient"  
    

    or for an AppToken

    $clientSecret = (ConvertTo-SecureString yourClientSecret -AsPlainText -Force )  
    $token = Get-MsalToken -clientID $clientID -clientSecret $clientSecret -tenantID $tenantID -Scopes "https://outlook.office365.com/.default"  
    

    then use

    $accessToken = $token.AccessToken

    1 person found this answer helpful.
    0 comments No comments