Step-by-Step Guide to Command-Line Management of Microsoft 365
Managing Microsoft 365 means you must fix problems fast. You can use tools like PowerShell, Microsoft Graph PowerShell, CLI for Microsoft 365, and the Office Deployment Tool. These tools work on many computers. They help you control things and do jobs automatically.
You may have problems like slow logs, DNS cache issues, and network errors. Command-line tools help you fix these problems quickly and correctly.
Key Takeaways
Command-line tools like PowerShell and Microsoft Graph PowerShell let you manage Microsoft 365 quickly. They work faster than the admin center.
You can use scripts to do the same task many times. This saves time and helps stop mistakes. Begin with easy scripts. Later, try harder jobs.
Command-line tools help you do big jobs at once. You can change many users or settings together. This makes your work easier and faster.
Always keep things safe. Use multi-factor authentication. Use safe ways to handle passwords in your scripts.
Try different tools to see which one works best for you. Each tool has special features. These features can make your work easier.
Why Use the Command-Line?
Efficiency and Automation
You want to manage Microsoft 365 in less time. Command-line tools help you do this job faster. They work quicker than the admin center. You can get information, make changes, and fix things fast. You do not need to wait for web pages to load.
Command-line tools let you automate jobs. You can write a script once. The script does the job for you every time. This saves you time and helps you avoid mistakes.
Here are some main reasons to use command-line tools instead of the admin center:
You can use scripts for jobs like making users, updating licenses, or making reports. For example, you can set up a script to send a report every week. You do not have to do the same steps each week.
When to Choose Command-Line
Use the command-line when tasks take too long in the admin center. If you need to manage many users or settings, the command-line is faster and works better.
Here are some times when command-line tools are best:
You also get more control over security and rules. For example, you can set up multi-factor authentication for all users. You can manage licenses for many people during company changes. You can even set reports to run and send by themselves.
Tip: Start with easy scripts. When you feel ready, you can automate harder jobs and save even more time.
Tools for Managing Microsoft 365
When you want to manage Microsoft 365 with commands, you need special tools. Each tool helps you with different jobs and makes things simpler. Here is what you should know about the main choices.
PowerShell Options
PowerShell lets you control Microsoft 365 in many ways. You can use modules to work with users, mail, sites, and teams. These modules help you do tasks fast and make fewer mistakes.
Azure Active Directory (Azure AD): You manage who can use accounts and what they can do.
Exchange Online PowerShell Module: You control mailboxes and set up Exchange options.
SharePoint Online PowerShell Module: You manage SharePoint sites and change settings.
Microsoft Teams PowerShell Module: You set up Teams and change how they work.
You can use these modules to do daily jobs, like adding users or changing permissions. This makes managing Microsoft 365 quicker and more dependable.
Microsoft Graph PowerShell
Microsoft Graph PowerShell lets you work with many Microsoft 365 services using one tool. You can automate jobs, keep your scripts the same, and make things safer. This tool helps you handle big groups of users or settings without doing each step yourself.
You save time by automating jobs you do a lot.
Your scripts always run the same way.
You make fewer mistakes and keep things safe.
You can update many users at once.
CLI for Microsoft 365
The CLI for Microsoft 365 works on Windows, macOS, and Linux. You can use it in different shells, like Bash or PowerShell. It makes logging in easy and helps you manage tenants and SharePoint projects.
You can install the CLI with npm and start managing Microsoft 365 right away.
Office Deployment Tool
The Office Deployment Tool helps you install and update Office apps on many computers. You can choose which apps to install, control updates, and use scripts to make things faster.
Tip: Try each tool to see which one works best for you. Managing Microsoft 365 gets easier when you use the right tool.
Setup and Connection
To start using command-line tools, you must set things up first. This helps you manage Microsoft 365 better. You need to get your computer ready, connect to your tenant, and make sure your login is safe.
Install Tools
Check if your computer meets the basic needs before you start. This helps stop problems later.
To add PowerShell modules, use this command:
Install-Module -Name ExchangeOnlineManagement
To get CLI for Microsoft 365, use npm:
npm install -g @pnp/cli-microsoft365
Update your tools often. This keeps your computer safe and working well.
Connect to Microsoft 365
After you install your tools, you need to connect to your Microsoft 365 tenant. Follow these steps to make a safe connection:
Load the Exchange Online PowerShell module:
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName <UPN>
Do multi-factor authentication if you need to.
Connect to Microsoft Graph:
Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"
Connect to SharePoint Online:
Connect-SPOService -Url https://<orgName>-admin.sharepoint.com -Credential $Credential
Connect to Exchange Online:
Connect-ExchangeOnline -ShowProgress $true
Connect to Teams PowerShell:
Connect-MicrosoftTeams -Credential $credential
When you are done, disconnect from all services:
Disconnect-SPOService; Disconnect-MicrosoftTeams; Disconnect-ExchangeOnline
Tip: Always disconnect when you finish. This keeps your computer safe.
Permissions and Authentication
You need strong ways to log in to keep your data safe in Microsoft 365. Here are some good tips:
Make users use MFA if they see important info.
Turn on self-service password reset (SSPR) so users can change their own passwords.
Doing these steps keeps your tenant safe. Using the right setup and login makes managing Microsoft 365 easier and safer.
Managing Microsoft 365: Essential Tasks
User Management
You do many jobs for users in Microsoft 365. You can add users, change details, or delete accounts. PowerShell and Microsoft Graph PowerShell help you work fast.
To make many users at once, do these steps:
Make a CSV file with DisplayName, UPN, MailNickName, and Password as headers.
Save your file in a safe spot.
Use this PowerShell script to add users from the CSV:
$users = Import-Csv -Path "Users.csv"
foreach ($user in $users) {
$userParams = @{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
MailNickname = $user.MailNickname
AccountEnabled = $true
PasswordProfile = @{
Password = $user.Password
ForceChangePasswordNextSignIn = $true
}
}
New-MgUser -BodyParameter $userParams
}
Passwords must be 8 to 16 characters long. They need small and big letters, numbers, and symbols.
You can use Microsoft365DSC to make many users at once. This tool lets you use code to automate user jobs.
Some common user jobs are looking at groups, making reports, and giving permissions. Here is a table with helpful commands:
Tip: Use scripts to save time and make fewer mistakes when you work with lots of users.
License Management
You need to check licenses so everyone has what they need. PowerShell commands help you look at, give, or take away licenses.
Here is a table with important cmdlets and what they do:
You can check license assignments in different ways:
Use Microsoft Entra Admin Center to look at license paths.
Run
Get-MgBetaUser
to see the LicenseAssignmentStates for each user.Download and run a PowerShell script to see if licenses are given by hand or by groups.
Note: Checking licenses often helps you save money and make sure everyone has what they need.
Group and Role Management
Groups and roles help you organize users and control who can do things. You can make, change, or delete groups with PowerShell. Here is a table of useful cmdlets:
To manage roles, do these steps:
Check current Microsoft 365 roles in your admin center.
Decide which roles and permissions your group needs.
Make custom roles if the built-in ones do not work.
Change role assignments when needed.
You can use PowerShell scripts to list and change roles in Azure AD, Exchange Online, and Intune. This helps you keep control over who can do things.
SharePoint, Exchange, Teams
You can manage SharePoint sites, Exchange mailboxes, and Teams settings with command-line tools. PowerShell makes these jobs faster and easier.
To connect and manage these services, do these steps:
Open Windows PowerShell.
Install the needed module, for example:
Install-Module -Name AzureAD
Import the module:
Import-Module AzureAD
Connect to your Microsoft 365 subscription:
Connect-AzureAD
You can use scripts for common jobs in each service:
For Exchange: Use
Connect-ExchangeOnline
andNew-UnifiedGroup
to make a new group.For Teams: Use
Connect-MicrosoftTeams
andNew-Team
to make a new team.For SharePoint: Use
Connect-SPOService
andNew-SPOSite
to make a new site.
To set up OAuth Trust and Service Permission on Exchange, run:
.\Configure-EnterprisePartnerApplication.ps1 -ApplicationType Sharepoint -AuthMetadataUrl https://<SP_FQDN>/_layouts/15/metadata/json/1
Here are some admin centers you can use for extra management:
Exchange Admin Center Manage Exchange Online settings and users .https://outlook.office365.com/ecp
New Exchange Admin Center Modern look for Exchange settings and moves.
https://admin.exchange.microsoft.com/
Teams Admin Center Manage Teams settings, devices, and apps.
https://admin.teams.microsoft.com/
SharePoint Online Admin Center Manage SharePoint sites and moves.https://admin.microsoft.com/sharepoint
Using these tools helps you automate jobs, keep things neat, and react to changes fast.
Automation and Best Practices
Scripting Tasks
You can save time by using scripts. PowerShell is the main scripting language for Microsoft 365. People also use Python, VBScript, JavaScript, and C#/VB.NET for automation. PowerShell is best for most jobs because it connects easily to Microsoft 365.
PowerShell
Python
VBScript
JavaScript
C#/VB.NET
Start with scripts for jobs you do a lot. For example, you can make a script to add users, update licenses, or make reports. Use loops to work with many accounts at once. You can set scripts to run at certain times with Task Scheduler or Azure Automation.
Here is a simple PowerShell script. It gets user details and saves them in a CSV file:
Get-MgUser | Select-Object DisplayName, UserPrincipalName | Export-Csv -Path "UserReport.csv" -NoTypeInformation
Tip: Test your scripts in a safe place first. This helps you avoid mistakes.
Security Tips
You must keep your scripts and passwords safe. Always use secure ways to handle passwords and important data. Ask for credentials with Get-Credential
so passwords are not saved in plain text. Store credentials in a PSCredential object. Use Export-Clixml
and Import-Clixml
to save and get them safely.
To protect passwords, encrypt them with ConvertFrom-SecureString
before saving. Only you can unlock the password, which makes it safer.
Use role-based access control so admins only get needed permissions.
Make strong password rules and use multi-factor authentication for all accounts.
Watch for strange activity, especially in admin accounts.
Limit PowerShell use to lower the risk of problems.
Here is a safe way to ask for credentials in PowerShell:
$credential = Get-Credential
Connect-ExchangeOnline -Credential $credential
Note: Never save passwords in plain text in your scripts. Always use safe ways to protect your data.
Command-line tools let you manage Microsoft 365 quickly. You make fewer mistakes when you use these tools. Start with easy jobs first. Later, you can use automation for bigger jobs. Many companies use automation for reports and meetings. They also use it for customer updates and tracking projects.
Getting ready for meetings and following up is quicker.
Customers get updates right when they need them.
Project status changes as tasks get done.
You can learn new skills by trying new tools. The Microsoft 365 Agents Toolkit CLI helps you make and manage apps for Microsoft 365.
Keep learning new things. Try new tools and scripts to make your work easier every day.
FAQ
How do you update PowerShell modules for Microsoft 365?
To update a module, open PowerShell. Type this command:
Update-Module -Name ModuleName
Change ModuleName
to the one you want to update. Always update your modules. This helps them work better and keeps them safe.
What should you do if a script fails to connect to Microsoft 365?
First, check if you have internet. Make sure your login is right. If you use multi-factor authentication, finish those steps. Try running the script as an admin. If it still does not work, read the error messages for help.
Can you automate daily reports with command-line tools?
Yes, you can! Write a script to get your data and save it. Use Task Scheduler or Azure Automation to run it each day. This saves time and makes sure you always get your report.
Is it safe to store passwords in scripts?
Never put passwords in plain text.
UseGet-Credential
in PowerShell to ask for your password. You can also use Windows Credential Manager or encrypted files to keep passwords safe.
Which tool should you start with as a beginner?
Start with PowerShell first. It has lots of guides and examples online. You can do most Microsoft 365 jobs with it. When you learn more, try Microsoft Graph PowerShell or CLI for Microsoft 365 for harder jobs.