Answered by:
Delete content + Folders of a mailbox in O365

Question
-
$Credential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session $Identity = "xxx@contoso.com" If (!($Identity)) { $Identity = Read-Host "Enter user mailbox to wipe" } Write-Host -Fore Green "Content from $Identity will be erased." ## Create Exchange Service Object Write-Host -ForegroundColor DarkGreen " Connecting to AutoDiscover endpoint for $($Credential.UserName)." $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013 $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion) $creds = New-Object System.Net.NetworkCredential($Credential.UserName.ToString(),$Credential.GetNetworkCredential().password.ToString()) $Service.Credentials = $creds $Service.AutodiscoverUrl($Credential.Username, {$true}) Get-MailboxFolderStatistics $identity | Select Identity, ItemsInFolder | ft Write-Host -Fore Yellow " Purging folders ..." $Service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Identity) $Root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root) $test = $identity + ":\" $Root = Get-MailboxFolder xxxx@contoso.com $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $FolderList = $Root.FindFolders($FolderView) ForEach ($Folder in $FolderList.Folders) { Write-Host -Fore DarkYellow " Processing $($Folder.DisplayName) ..." Try { $Folder.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) } Catch { [System.Exception] | Out-Null } Finally { } } # Deleting remaining inbox content via Search-Mailbox cmdlet Write-Host -Fore Yellow " Purging content ..." $identity = "xxx@contoso.com" Search-Mailbox -Identity $Identity -DeleteContent -Force Errors: Unable to find type [Microsoft.Exchange.WebServices.Data.ExchangeVersion]. At line:15 char:20 + ... geVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Ex ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Excha...ExchangeVersion:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound New-Object : Cannot find type [Microsoft.Exchange.WebServices.Data.ExchangeService]: verify that the assembly containing this type is loaded. At line:16 char:12 + $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeSer ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Hello guys,
I'm trying to delete all the content and folders of a mailbox with the above script... but I'm receiving those errors..
Could you please help me to understand what's wrong?
Thanks
Wednesday, July 19, 2017 7:44 AM
Answers
-
I think you need to remove (or comment out) line 28 $root = get-mailboxfolder....
$root already gets defined 2 lines earlier with the Exchange webservice.
- Marked as answer by Ivan LUX Wednesday, July 19, 2017 11:35 AM
Wednesday, July 19, 2017 10:58 AM
All replies
-
Hi Ivan,
I would change $usercredential to $credential in the second line to prevent another prompt :)
In this script I miss the import of the Exchange Web Services managed API. You can find it here:
https://www.microsoft.com/en-us/download/details.aspx?id=42951
after installation you will have to import it with import-module before creating the Exchange Service Object.
Import-Module -Name "c:\program files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Good luck!
Wednesday, July 19, 2017 8:53 AM -
Hello,
Many thanks for your answer!
The previous error seems gone, but I receive a new one:
Purging folders ... Exception calling "Bind" with "2" argument(s): "The account does not have permission to impersonate the requested user." At line:26 char:1 + $Root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServiceResponseException The specified mailbox "xxxx@contoso.com" doesn't exist. + CategoryInfo : NotSpecified: (:) [Get-MailboxFolder], ManagementObjectNotFoundException + FullyQualifiedErrorId : [Server=DB3PR05MB490,RequestId=d2de5041-8a9d-48c1-89e8-eef9eba42290,TimeStamp=19/07/2017 09:11:38] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 8C409017,Microsoft.Exchange.Management.StoreTasks.GetMailboxFolder + PSComputerName : outlook.office365.com You cannot call a method on a null-valued expression. At line:33 char:1 + $FolderList = $Root.FindFolders($FolderView) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Any idea about this?
Thank again!
Wednesday, July 19, 2017 9:28 AM -
You should give the account you use permissions in the Exchange Admin Center:
Add ApplicationImpersonation to the Discovery Management role and add the adminaccount you use to the Discovery Managment role like described here; http://cloudfinder.com/user-impersonation-settings-office-365/
And you probably want to look at lines 27 and 28... because xxxx@contoso.com is probably not the mailbox you want :)
Wednesday, July 19, 2017 9:44 AM -
Hello again,
and thanks .. you are saving me:D
Now it seems working.. I just receiving an error when trying to delete the folders..
$Credential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication Basic -AllowRedirection Import-PSSession $Session $Identity = "username@mycompany.com" If (!($Identity)) { $Identity = Read-Host "Enter user mailbox to wipe" } Write-Host -Fore Green "Content from $Identity will be erased." ## Create Exchange Service Object Write-Host -ForegroundColor DarkGreen " Connecting to AutoDiscover endpoint for $($Credential.UserName)." Import-Module -Name "c:\program files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016 $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion) $creds = New-Object System.Net.NetworkCredential($Credential.UserName.ToString(),$Credential.GetNetworkCredential().password.ToString()) $Service.Credentials = $creds $Service.AutodiscoverUrl($Credential.Username, {$true}) Get-MailboxFolderStatistics $identity | Select Identity, ItemsInFolder | ft Write-Host -Fore Yellow " Purging folders ..." $Service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Identity) $Root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root) $test = $identity + ":\" $Root = Get-MailboxFolder -Identity username@mycompany.com $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $FolderList = $Root.FindFolders($FolderView) ForEach ($Folder in $FolderList.Folders) { Write-Host -Fore DarkYellow " Processing $($Folder.DisplayName) ..." Try { $Folder.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) } Catch { [System.Exception] | Out-Null } Finally { } } # Deleting remaining inbox content via Search-Mailbox cmdlet Write-Host -Fore Yellow " Purging content ..." $identity = "username@mycompany.com" Search-Mailbox -Identity $Identity -DeleteContent -Force Error: Purging folders ... The specified mailbox "username@mycompany.com" doesn't exist. + CategoryInfo : NotSpecified: (:) [Get-MailboxFolder], ManagementObjectNotFoundException + FullyQualifiedErrorId : [Server=DB3PR05MB490,RequestId=8611b9ad-5717-4485-927a-29cce8f16f35,TimeStamp=19/07/2017 10:03:50] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 8C409017,Microsoft.Exchange.Management.StoreTasks.GetMailboxFolder + PSComputerName : outlook.office365.com You cannot call a method on a null-valued expression. At line:43 char:1 + $FolderList = $Root.FindFolders($FolderView) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Everything runs as charm.. until I receive that error.. but the mailbox is the same! I dont understand..
1000 thanks!
Wednesday, July 19, 2017 10:09 AM -
I think you need to remove (or comment out) line 28 $root = get-mailboxfolder....
$root already gets defined 2 lines earlier with the Exchange webservice.
- Marked as answer by Ivan LUX Wednesday, July 19, 2017 11:35 AM
Wednesday, July 19, 2017 10:58 AM -
Thaaaanks a lot!!! Have a great day:)Wednesday, July 19, 2017 11:36 AM