logo
search
License & Subscription Issues

How to Fix PowerShell Errors When Disabling Microsoft 365 License Plans

Elise WilliamsElise Williams Oct 10, 2026 868 views

Question details

The user is attempting to disable specific service plans within a Microsoft 365 E3 license using a PowerShell script but is encountering an operation error.

How to Fix PowerShell Errors When Disabling Microsoft 365 License Plans
Product
Microsoft 365
Device & OS
not provided
Scenario
Running an administrative PowerShell script to automate the removal or disabling of certain Microsoft 365 E3 service plans for users.
Observed behavior
The script fails and returns an OperationNotAllowedException due to variable-name mismatches and spelling errors in the code.
Before you start

Ensure you have the necessary global administrator or user administrator privileges in your Microsoft 365 tenant, and open your script in an IDE like PowerShell ISE or VS Code to easily spot syntax errors.

Solution 1Recommended

Correct Variable Name Mismatches and Syntax Typos

Fixing common scripting mistakes, such as mismatched variable names and typos, will allow the script to execute without throwing the OperationNotAllowedException.

In PowerShell, referencing an undefined or incorrectly named variable passes a null value to your command, which often triggers an OperationNotAllowedException when interacting with Microsoft 365 user objects.

1
Standardize user object variables

Check your loop declarations. If you store the user object in a variable named $Acct, you must reference it exactly as $Acct.UserPrincipalName on line 6, rather than using $User.UserPrincipalName.

2
Correct loop commands

Scan the script for standard PowerShell cmdlets and loop statements. Look for typos like 'forearch' and correct them to the proper syntax, which is 'foreach'.

3
Verify license variable characters

Check your license option variables for confusing characters. Ensure you are using the intended letters (e.g., $LO with the letter O) rather than numbers (e.g., $L0 with a zero).

4
Run and test the script

Save your changes and execute the script again against a single test user to verify that the E3 service plans are successfully disabled without exceptions.

Correct Variable Name Mismatches and Syntax Typos
Additional Support: If you continue to experience scripting issues after correcting these errors, consider posting your sanitized script to the Microsoft PowerShell Community or Microsoft Q&A for advanced assistance.
Free Microsoft Office alternative

Avoid complex licensing with WPS Office

Managing Microsoft 365 enterprise licenses via PowerShell can be complicated and prone to administrative errors. If you or your team simply need powerful, reliable productivity software without the hassle of complex tenant licensing, WPS Office is an excellent alternative. It provides everything you need to create, edit, and share documents effortlessly.

Fully compatible with Microsoft Office formats including .docx, .xlsx, and .pptx.Free to use without requiring complex PowerShell scripts to manage user service plans.Lightweight installation that runs smoothly on Windows, Mac, Linux, and mobile devices.Familiar tabbed interface ensures a zero-learning-curve migration for your team.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Microsoft 365 PowerShell script return an OperationNotAllowedException?

This error typically occurs when the script attempts to perform an action using invalid or empty parameters. In license management scripts, this usually happens if a variable (like $User) is called but hasn't been defined, passing a null identity to the license modification command.

How do I properly reference a user's UPN in a foreach loop?

The variable used inside the loop must exactly match the item variable declared in the loop statement. For example, if you use 'foreach ($Acct in $Users)', you must use '$Acct.UserPrincipalName' inside the loop block to successfully retrieve the User Principal Name.

Can I disable specific service plans without using PowerShell?

Yes, you can manually disable specific service plans for individual users via the Microsoft 365 admin center by going to Active Users, selecting a user, navigating to the Licenses and Apps tab, and unchecking the specific apps under the Apps section. However, PowerShell is recommended for bulk operations.