Connect to Exchange Online PowerShell
Exchange Online PowerShell Modunle
You need install Exchange Online module only once. In the next time, run to connect to Exchange Online PowerShell.
##Connect to Exchange Online Powershell.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
Install-Module -Name ExchangeOnlineManagement;
Import-Module ExchangeOnlineManagement;
Connect-ExchangeOnline;
Add impersonation rights in Exchange Online
$acctName = Read-Host -Prompt "Enter your admin account"
New-ManagementRoleAssignment -Role "ApplicationImpersonation" -User $acctName
Install Microsoft Exchange Web Services Managed API 2.2
mkdir C:\temp; cd C:\temp;
curl -o C:\temp\EwsManagedApi_2.2.msi "https://filedn.com/lOX1R8Sv7vhpEG9Q77kMbn0/scripts/m365/others/EwsManagedApi_2.2.msi";
curl -o C:\temp\delete-folders-exo.ps1 "https://filedn.com/lOX1R8Sv7vhpEG9Q77kMbn0/scripts/m365/delete-folders-exo.ps1";
start-process "C:\temp\EwsManagedApi_2.2.msi" -ArgumentList "/quiet"
Create text files for the script
New-Item -Path "C:\temp\Folders.txt" -ItemType File;
New-Item -Path "C:\temp\Log.txt" -ItemType File;
New-Item -Path "C:\temp\Users.txt" -ItemType File;
Add-Content "C:\temp\Folders.txt" "FolderName";
Add-Content "C:\temp\Users.txt" "EmailAddress";
ii C:\temp;
notepad.exe .\Folders.txt;
notepad.exe .\Users.txt
Delete Folders Exchange Online PowerShell script
.\delete-folders-exo.ps1
Edit the Delete Folders Exchange Online PowerShell script
The script will delete the folders completely, and you can’t recover them from deleted items. Do you want to delete the folders to the mailbox Deleted Items folder?
Check lines 78, 79, and 80 for the available options. Change it to your needs.
# You can choose from a few delete types, just choose one:
$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
#$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
#$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
The script will target the mailboxes specified in the Users.txt file. Do you want to target all the mailboxes?
Change line 102 from:
Import-Csv $UsersCSV -Encoding UTF8 | Foreach-Object {
to
Get-Mailbox -ResultSize Unlimited | Foreach-Object {
5/5 - (1 vote)