How to Fix PowerShell Errors When Disabling Microsoft 365 License Plans
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.

- 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.
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.
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.
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.
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'.
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).
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.

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.

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.




