Office 365 PowerShell is the go-to tool for administrators to automate tasks like managing users, licenses, and groups in Microsoft 365. With a single command, you can add/remove accounts, configure security settings, or deploy policies—saving hours of manual work. Before you begin, ensure you’re using the correct module, which requires an active admin account with the necessary permissions in Office 365.

What You'll Need

  • Windows PowerShell (version 5.1 or later) installed on your PC
  • The Microsoft Online Services Sign-In Assistant installed (required for authentication)
  • An Office 365 admin account with appropriate permissions (e.g., Global Admin, User Admin)
  • The Microsoft.Graph or Exchange Online PowerShell modules (install via `Install-Module -Name Microsoft.Graph` or `Install-Module -Name ExchangeOnlineManagement`)
  • Admin credentials for the target Office 365 tenant (domain and password)

Step-by-Step

  1. Connect to Office 365 Open PowerShell as Administrator and run: Connect-MsolService -Credential (Get-Credential) A browser window will prompt you to authenticate. Enter your admin credentials and grant permissions. Note: Labels may vary slightly by tenant or version. If prompted, accept default settings.
  2. Verify connection Check if the session is active by running: Get-MsolUser -All | Select-Object DisplayName, UserPrincipalName This lists all users in your tenant. If no users appear, reconnect with the correct admin permissions.
  3. Add a new user To create a user, use: New-MsolUser -UserPrincipalName user@domain.com -DisplayName "John Doe" -PasswordRequiredAtSignIn $true -PasswordNeverExpires $false Replace user@domain.com with the target email. Verify the user was added with: Get-MsolUser -UserPrincipalName user@domain.com
  4. Assign a license To attach a license (e.g., Exchange Online Plus) to the user: Assign-MsolUserLicense -UserPrincipalName user@domain.com -AddLicenses "EXOPPLUS" Confirm the assignment with: Get-MsolUserLicenseAssignment -UserPrincipalName user@domain.com
  5. Manage groups Create a security group via PowerShell: New-MsolGroup -DisplayName "DevTeam" -MailNickname "DevTeam" Add users to the group: Add-MsolGroupMember -GroupObjectId "" -UserPrincipalNames user1@domain.com,user2@domain.com Retrieve the group ID by running: Get-MsolGroup -All | Select-Object ObjectId,DisplayName
  6. Reset a password Force a password reset for a user: Set-MsolUserPassword -UserPrincipalName user@domain.com -NewPassword (ConvertTo-SecureString -String "TempPass123" -AsPlainText -Force) Users will receive an email to change it later.
  7. Export user data (optional) Backup user details to CSV: Get-MsolUser -All | Export-Csv -Path "C:\Temp\Users.csv" -NoTypeInformation This file can be imported later.
  8. Disconnect safely Exit the session with: Disconnect-MsolService Clear cached credentials if needed: Remove-MsolServicePrincipalApplication -ApplicationId ""

If That Did Not Work

If commands fail, check these before troubleshooting:
  1. Authentication issues Verify the admin account has Global Admin rights. Reset credentials via: Set-MsolUser -UserPrincipalName admin@domain.com -PasswordNeverExpires $true -PasswordRequiredAtSignIn $true
  2. Module errors Reinstall the module: Remove-Module Microsoft.Graph -Force; Install-Module Microsoft.Graph Update PowerShell: Update-Package -All
  3. Permissions denied Ensure the tenant supports the action (e.g., license assignment may require a usage agreement).
  4. Network restrictions Check if your organization blocks outbound connections to Microsoft APIs.

Common Mistakes

  • Wrong tenant context Always specify the correct tenant (e.g., `-TenantId "contoso.onmicrosoft.com"`). Missed this, and commands apply to the wrong account.
  • Permissions gap Global Admin tasks fail if you lack Global Admin rights. Use: Get-MsolUser -All | Select-Object UserPrincipalName,LicenseStatus to check licenses.
  • SecureString issues When passing passwords, use `-PasswordNeverExpires $true` and avoid hardcoding. Errors like "SecureString not supported" indicate a version mismatch.
  • Modifying properties incorrectly Use `Get-MsolUser -All` to preview changes before applying them. Example: DisplayName requires a valid name; invalid values trigger failures.

Tips

  • Batch operations Process multiple users in one cmdlet by piping results: Get-MsolUser -All | ForEach-Object { Add-MsolGroupMember -GroupObjectId "Group-ID" -UserPrincipalName $_.UserPrincipalName }
  • Use aliases for speed Add these to your PowerShell profile: Set-Alias msol Connect-MsolService; Set-Alias getuser Get-MsolUser
  • Audit changes Enable logging in Azure AD for visibility: Set-MsolServicePrincipal -ApplicationId "" -AuditLogEnabled $true
  • Backup before changes Always export user data (`Export-Csv`) before modifying permissions or licenses.
  • Schedule with Task Scheduler Store PowerShell scripts in a secure location and run them via Task Scheduler with admin rights.

Troubleshooting

  1. Connection timed out Verify your network allows outbound traffic to `Graph.microsoft.com` and `login.microsoftonline.com`. Use a VPN if needed.
  2. 403 Forbidden errors Check tenant settings in Microsoft 365 Admin Center > Settings > Services & add-ons. Ensure the service is enabled.
  3. Permission denied For custom app permissions, request them via Azure AD App Registrations. Use: Get-MsolServicePrincipal -ApplicationId "" | Select-Object ApplicationId, SignInAudience
  4. Module not found Install the Exchange Online module for Exchange-specific tasks: Install-Module ExchangeOnlineManagement -Scope CurrentUser

Why does my script hang?

A timeout usually means network issues or throttling. Increase the timeout with: $psSession = New-PSSession -ConfigurationName Microsoft.Online.Administration.Center -ConnectionUri "https://yourtenant-admin.microsoft.com" -Credential admin@domain.com -UseSecureChannel -TimeoutSec 600

Can I use this for non-admin accounts?

No. Only tenant admins can run these commands. For lower permissions, use the Graph Explorer tool (download from Microsoft) to test APIs manually.

What if PowerShell blocks my commands?

Run as Admin, disable execution policies if needed: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

How do I find my tenant ID?

Log into Microsoft 365 Admin Center, then: Get-MsolTenant | Select-Object TenantId

Will this work across all Office 365 plans?

Most commands apply to all plans, but some (e.g., license assignments) require a supported plan (e.g., Enterprise E3/E5).

How to undo a failed change?

If you modified a user or group, remove the item by running the reverse cmdlet (e.g., Remove-MsolUser for a deleted user). For groups, use Remove-MsolGroup.

PowerShell is your fastest path to Office 365 automation, but always validate changes incrementally to avoid unintended disruptions. For complex configurations, document your steps or use a backup before running bulk operations.