How to Bulk Disable or Enable Microsoft 365 User Accounts with Graph PowerShell

Managing user accounts in Microsoft 365 (M365) can be time-consuming, especially for large organizations. Learn how to use Graph PowerShell to bulk disable or enable user accounts, streamlining your administrative tasks and saving valuable time.


Prerequisites

  • You should install Microsoft Graph PowerShell module by running Install-Module Microsoft.Graph -Scope CurrentUser command.
  • You should connect to Microsoft Graph PowerShell module by running Connect-MgGraph -Scopes "User.ReadWrite.All".
  • You should atleast have the Microsoft 365 user administrator role assigned to you.
  • A CSV file containing the user principal names (UPNs) of the accounts you wish to enable or disable.

Preparing the CSV File

Prepare a CSV file (users.csv) with the following structure:


Disabling User Accounts in Bulk

To disable user accounts in bulk, you need to set the accountEnabled property to false. Here's the Graph PowerShell script to do this:





Enabling User Accounts in Bulk

To enable user accounts in bulk, you need to set the accountEnabled property to true. Here's a Graph PowerShell script to do this:




Frequently Asked Questions (FAQs)


What permissions do I need to run these scripts?

You need to have the "User.ReadWrite.All" permission to manage user accounts using Graph PowerShell. You should also have the Microsoft 365 Administrator role assigne to you.

Can I use these scripts to manage guest user accounts?

Yes, you can manage both regular and guest user accounts with these scripts, as long as you have the necessary permissions

How can I log errors to a file?

You can modify the scripts to log errors to a file by using the Out-File cmdlet. For example: Failed to disable account for user: $($user.UserPrincipalName). Error: $_" | Out-File -FilePath "C:\path\to\errorlog.txt" -Append

What if some user accounts are not found?

Ensure that the UPNs in your CSV file are correct and exist in your tenant. The scripts will skip any accounts that are not found and log the error.


Conclusion


Using Graph PowerShell, you can efficiently manage user accounts in Microsoft 365, enabling or disabling them in bulk with ease. This approach saves time and reduces the potential for manual errors, making it an essential tool for administrators.

Note: Always stay updated by reading the Microsoft documentation: Disable M365 User Accounts


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex