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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.