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
- 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. - Verify connection
Check if the session is active by running:
Get-MsolUser -All | Select-Object DisplayName, UserPrincipalNameThis lists all users in your tenant. If no users appear, reconnect with the correct admin permissions. - Add a new user
To create a user, use:
New-MsolUser -UserPrincipalName user@domain.com -DisplayName "John Doe" -PasswordRequiredAtSignIn $true -PasswordNeverExpires $falseReplaceuser@domain.comwith the target email. Verify the user was added with:Get-MsolUser -UserPrincipalName user@domain.com - 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 - Manage groups
Create a security group via PowerShell:
New-MsolGroup -DisplayName "DevTeam" -MailNickname "DevTeam"Add users to the group:Add-MsolGroupMember -GroupObjectId "Retrieve the group ID by running:" -UserPrincipalNames user1@domain.com,user2@domain.com Get-MsolGroup -All | Select-Object ObjectId,DisplayName - 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. - Export user data (optional)
Backup user details to CSV:
Get-MsolUser -All | Export-Csv -Path "C:\Temp\Users.csv" -NoTypeInformationThis file can be imported later. - Disconnect safely
Exit the session with:
Disconnect-MsolServiceClear cached credentials if needed:Remove-MsolServicePrincipalApplication -ApplicationId ""
If That Did Not Work
If commands fail, check these before troubleshooting:- Authentication issues
Verify the admin account has Global Admin rights. Reset credentials via:
Set-MsolUser -UserPrincipalName admin@domain.com -PasswordNeverExpires $true -PasswordRequiredAtSignIn $true - Module errors
Reinstall the module:
Remove-Module Microsoft.Graph -Force; Install-Module Microsoft.GraphUpdate PowerShell:Update-Package -All - Permissions denied Ensure the tenant supports the action (e.g., license assignment may require a usage agreement).
- 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,LicenseStatusto 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:
DisplayNamerequires 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
- Connection timed out Verify your network allows outbound traffic to `Graph.microsoft.com` and `login.microsoftonline.com`. Use a VPN if needed.
- 403 Forbidden errors Check tenant settings in Microsoft 365 Admin Center > Settings > Services & add-ons. Ensure the service is enabled.
- Permission denied
For custom app permissions, request them via Azure AD App Registrations. Use:
Get-MsolServicePrincipal -ApplicationId "" | Select-Object ApplicationId, SignInAudience - 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.