none
Powershell Office 365 - Delete content + folders of a mailbox RRS feed

  • Question

  • Hello Scripting guys,

    Hello O365 experts,

    I'm trying to delete all the content including the folders of a mailbox with the following powershell script, but I'm getting an error.

    $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 = "username@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 username@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 = " username@contoso.com "
    Search-Mailbox -Identity $Identity -DeleteContent -Force
    
    Error:
    
    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

    Is there anyone can advice me? Or better an alternative script to delete the content+folders...

    Many thanks to all

    • Moved by Bill_Stewart Wednesday, September 13, 2017 8:53 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, July 19, 2017 7:48 AM

All replies

  • Look in the Gallery for scripts that will show you how to access Exchange online. You are mixing local Exchange with O365.  You need to use only EWS commands to access mailboxes.

    With O365 you can use Search-Mailbox to delete all of the contents of a mailbox and use folder commands to remove some folders. 


    \_(ツ)_/


    • Edited by jrv Wednesday, July 19, 2017 5:30 PM
    Wednesday, July 19, 2017 5:29 PM