Author Posts

November 3, 2017 at 5:26 pm

Little background: I work for an MSP, been tasked with creating a script that enables litigation hold for all users with a valid license that can be used across multiple tenants.

As I'm sure there are some scripts online that can accomplish just this task I am one who needs to trial and error to better understand why it works, which leads me to attempting to make my own from scratch. I appreciate the input from those in the community and am very interested in the methods others would implement into their own version of this script. I know there is a lot more power I can be utilizing within powershell and I would be interested to know if there is a way I can make this better/efficient/prettier/cooler/anything. Its a very short script but its one they would like me to implement in production so it was of some importance I get some input from other professionals.

#Gather license subscriptions for tenant. Look for Enterprise subscriptions that allow for litigation hold
$siteLic = Get-MsolAccountSku | Where{$_.AccountSkuID -match "ENTERPRISE"}
If($siteLic)
{
    #Get list of Office 365 users that are enabled and licensed.
    $oUsers = Get-MsolUser -EnabledFilter EnabledOnly | where{$_.IsLicensed -eq $true}
    Foreach($o in $O365Users)
    {
        #Of the licenses assigned to each user, find those that have an enterprise license.
        If($o.Licenses.AccountSkuID -match "ENTERPRISE")
        {

           #Enable litigation hold indefinitely 
           Set-Mailbox -Identity $o.UserPrincipalName -LitigationHoldEnabled $true -WhatIf

        }# /if
        Else{ Write-Verbose "$($o.DisplayName) is not assigned a valid license for litigation hold."}

    }# /foreach

}# /if
Else{ Write-Error "Tenant does not have the correct license subscriptions to enable litigation hold on mailboxes." }