All can someone help me proof this script. The goal is to easily and efficiently remove unlicensed users from the o365 distribution lists.
<# Remove Unlicensed Distribution Group Members #> $unlicensed= $unlicensed=Get-MsolUser -all | ? {$_.islicensed -eq $false} <# Gets a list of unlicensed users #> $unlicensed= $unlicensed | Select @{N="WindowsLiveID"; E={$_.userprincipalname}} <#create a new field called windowsliveid #> $groups= Get-DistributionGroup $dgroup=Foreach ($g in $groups) { Get-DistributionGroupMember $g.Name | Select Name,RecipientType,windowsliveid, @{N="DistroName";E={"$($g.name)"}} , @{N="DistroPrimarySMTPAddress"; E={"$($g.primarysmtpaddress)"}} } <#loop through each group and grab the members #> $dgroup= $dgroup | ? {$_.recipienttype -ne "MailContact"} <#easily filter out "MailContacts" #> $comparison= Compare-Object -ReferenceObject $dgroup -DifferenceObject $unlicensed -Property windowsliveid -PassThru -IncludeEqual | ? {$_.sideindicator -eq "=="} <# list only unlicensed users and their distribution group #> Foreach ($c in $comparison) {Remove-DistributionGroupMember $c.DistroPrimarySMTPAddress -Member $c.WindowsLiveID -WhatIf} <#loop through and remove these users from the distribution group #>
To confirm these results i looped through the $comparison to confirm that there are no licensed users there.
submitted by /u/RampageUT
[link] [comments]
The post Remove Unlicensed Users from Distrobution Groups O365 appeared first on How to Code .NET.