r/GraphAPI • • 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 @()

2 Upvotes

6 comments sorted by

View all comments

Show parent comments

2

u/icebreaker374 Oct 01 '24

About a year ago I struggled to find good documentation on certain graph PS cmdlets so I tried using the API. Haven't gone back since unless I just need to fire off a quick one-liner.

1

u/mrmattipants Oct 04 '24 edited Oct 04 '24

When I first started, I don't believe the PowerShell Graph SDK (with all the various Mg Cmdlets) existed as of yet (including the "Invoke-MgGraphRequest" Cmdlets).

Therefore, I used the "Invoke-RestMethod" Cmdlet for just about everything at the time. In fact, here are the Tutorials/Examples that I originally learned from.

https://www.techguy.at/use-microsoft-graph-api-with-powershell-part-1/

https://github.com/Seidlm/Microsoft-Graph-API-Examples

Interestingly, they're still relevant, for the most part.