There's more...

Whenever you are executing code that can perform a destructive operation, it makes sense to implement the ShouldProcess method introduced with PowerShell v2 advanced functions. Implementing ShouldProcess in an advanced function gives you the ability to add the common risk mitigation parameters such as -Whatif and -Confirm. The following function takes our previous code up a notch, written as an advanced function that implements ShouldProcess. Add the following function to your Exchange Management Shell session:

 function RemovemailboxItem { [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "High" )] param( [Parameter( Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true )] [String] $PrimarySmtpAddress, ...

Get Microsoft Exchange Server 2016 PowerShell Cookbook - Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.