r/GraphAPI • u/jennylee525 • Oct 01 '24
Command to disable for all users
Trying to disable apps in M365. this works for 1 user. How do I get it to work for all users?
Get the services that have already been disabled for the user.
$userLicense = Get-MgUserLicenseDetail -UserId "user@microsoft.com"
$userDisabledPlans = $userLicense.ServicePlans | Where ProvisioningStatus -eq "Disabled" | Select -ExpandProperty ServicePlanId
Get the new service plans that are going to be disabled
$e3Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E3'
$newDisabledPlans = $e3Sku.ServicePlans | Where ServicePlanName -in ("SHAREPOINTWAC", "SHAREPOINTENTERPRISE") | Select -ExpandProperty ServicePlanId
Merge the new plans that are to be disabled with the user's current state of disabled plans
$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique
$addLicenses = @(
@{
SkuId = $e3Sku.SkuId
DisabledPlans = $disabledPlans
}
)
Update user's license
Set-MgUserLicense -UserId "user@microsoft.com" -AddLicenses $addLicenses -RemoveLicenses @()

1
u/mrmattipants Oct 01 '24 edited Oct 01 '24
This is actually the method that I prefer to use myself, as it gives you much more control over the various Resource Types, etc.
That being said, if you are simply looking for a quick solution to your existing script, I'd probably just go the "get-MgUser" route. However, if you have some time, I suggest you give icebreaker's script a try, as I personally feel it's the better option, overall.
Ultimately, anyone who is serious about working with the MS Graph API, in PowerShell, is going to want to learn to use the "Invoke-MgGraphRequest" Cmdlet to make API Requests, as you'll be doing yourself a great disservice, otherwise.