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