Complete Microsoft 365 PowerShell Environment Setup

⚙️Scripts & Automation

PowerShell  ·  Microsoft 365  ·  Microsoft Graph  ·  Exchange Online  ·  2026

🧰
This is a consultant-style full toolkit, not a minimal admin build. The script installs all 10 modules in one go — including heavier ones like Microsoft365DSC, ORCA and the Power Apps modules. If you are an internal admin who only works with Exchange and Teams, you may not need everything here. The script is designed for environments where the full M365 surface is in scope.

Every Microsoft 365 administrator or consultant has been through it: new machine, new project, or a colleague asking for help — and the first half hour disappears installing modules one by one, dealing with version conflicts, discovering that AzureAD is deprecated, or realising that PSGallery isn't configured as trusted. It's repetitive work that adds no value.

This script solves exactly that. In a single run, it verifies prerequisites, installs or updates all essential M365 modules, optionally removes the legacy modules that cause conflicts, and sets up your PowerShell profile with a Connect-M365 function that connects to every service — Graph, Exchange, SharePoint, Teams, and more — with one line.

📦
10 modules installed or updated automatically. The script covers Microsoft.Graph, ExchangeOnlineManagement, SharePoint Online, PnP.PowerShell, MicrosoftTeams, Power Apps, ORCA, Microsoft365DSC and MSCommerce — with version checking for each one.
🗑️
Optional removal of deprecated modules. AzureAD, AzureADPreview and MSOnline were deprecated by Microsoft from March 30, 2024. The script offers to remove them to prevent conflicts with the Microsoft Graph SDK, which is the official replacement.
PowerShell profile with Connect-M365. Beyond the installation, the script can create a profile with three functions — Connect-M365, Disconnect-M365 and Get-M365ModuleStatus — plus quick aliases. Once set up, connecting to every service is a single line.
🔐
Adapts to available privileges. When run as Administrator, it installs for all users (AllUsers). Without elevation, it installs for the current user only (CurrentUser) without failing.

Prerequisites

The script automatically checks everything it needs before installing anything. There is no manual environment preparation required — that is precisely the starting point.

💡
PowerShell 7.x is recommended, but the script works from 5.1 onwards. If the environment is still on 5.1, the script warns and continues — it does not block. For a modern M365 admin environment, migrating to PS 7.x is worthwhile, particularly for better support of the Microsoft Graph SDK.
Requirement Check Behaviour if missing
PowerShell 7.x Automatic WARN if 5.1, ERROR and exit if lower
Administrator privileges Automatic WARN — installs to CurrentUser instead of AllUsers
NuGet Provider ≥ 2.8.5.201 Automatic Installed automatically
PSGallery as Trusted Automatic Configured automatically
Internet connectivity Implicit Module downloads fail

Installed modules

The script covers the 10 modules that make up a complete M365 environment for administration and consulting. For each one, it checks whether a version is already installed — if so, it compares against the latest version on PSGallery and updates only if needed. If not present, it installs from scratch.

Module Service Notes
Microsoft.Graph Microsoft Graph API Meta-module that includes all Graph sub-modules. Replaces AzureAD and MSOnline for user, group, Entra ID, mail management and more.
ExchangeOnlineManagement Exchange Online & Security/Compliance Includes Connect-ExchangeOnline and Connect-IPPSSession for the Security & Compliance Center.
Microsoft.Online.SharePoint.PowerShell SharePoint Online SharePoint tenant administration — sites, site collections, tenant-level settings.
PnP.PowerShell SharePoint / Teams PnP Provisioning & Management — more granular than the native SPO module. Used for provisioning, templates and advanced automation.
MicrosoftTeams Microsoft Teams Teams administration — policies, channels, meetings, tenant-level configuration.
Microsoft.PowerApps.Administration.PowerShell Power Platform Environment management, DLP policies, and tenant-level Power Platform administration.
Microsoft.PowerApps.PowerShell Power Apps Maker tools — app, flow and connector management at the user level.
ORCA Exchange Online / Defender Office 365 ATP Recommended Configuration Analyzer. Generates a security recommendations report for Exchange Online and Defender for Office 365.
Microsoft365DSC M365 (all services) Desired State Configuration for M365 — useful for configuration auditing, exporting tenant state as code, and compliance reporting.
MSCommerce M365 Licensing License and subscription management — includes control over Self-Service Purchase by end users.
⚠️
Microsoft.Graph is a meta-module. Installing it causes PSGallery to install all Graph sub-modules (Microsoft.Graph.Users, Microsoft.Graph.Groups, etc.). The initial installation can take several minutes and occupy significant disk space. Subsequent updates are faster because only changed sub-modules are downloaded.

Legacy modules: AzureAD and MSOnline

The script asks whether you want to remove the AzureAD, AzureADPreview and MSOnline modules. The answer depends on context, but on new machines or environments that have already migrated to the Graph SDK, removal is recommended.

AzureAD, AzureADPreview and MSOnline were deprecated by Microsoft from March 30, 2024, with limited support continuing through March 30, 2025. Keeping them installed alongside Microsoft.Graph can cause type conflicts, cmdlet name collisions between modules, and unexpected behaviour when PowerShell resolves which version to invoke. Microsoft Graph PowerShell is the official replacement and covers the modern administration path for the scenarios previously handled by those legacy modules — but migration requires careful script review, as the syntax is not a direct 1:1 replacement.

Deprecated module Graph SDK replacement Status
AzureAD Microsoft.Graph.Users, Microsoft.Graph.Groups, Microsoft.Graph.Identity.DirectoryManagement Deprecated
AzureADPreview Microsoft.Graph.* (depends on feature) Deprecated
MSOnline Microsoft.Graph.Identity.DirectoryManagement, Microsoft.Graph.Users Deprecated
💡
If you have existing scripts that use AzureAD or MSOnline, do not remove the modules yet — migrate those scripts first. The Microsoft Graph SDK uses a different syntax, and direct cmdlet replacement requires careful review. Microsoft published a detailed migration guide at learn.microsoft.com.

The Connect-M365 function

The most useful part of the script is not the installation — it is what comes after. The script offers to configure your PowerShell profile with three functions that are available in every future session.

Connect-M365

Connects to all M365 services at once, or only to the ones you specify. It eliminates the need to remember Connect-ExchangeOnline, Connect-MgGraph, Connect-SPOService and the rest — everything in one command.

PowerShell — Basic usage
# Connect to all services
Connect-M365 -UPN admin@contoso.com

# Connect to Graph and Exchange Online only
Connect-M365 -UPN admin@contoso.com -ServicesOnly Graph,EXO

# Quick alias
cm365 -UPN admin@contoso.com

The -ServicesOnly parameter accepts any combination of: Graph, EXO, SPO, Teams, PnP, SEC. If omitted, the function connects to all of them.

Disconnect-M365 and Get-M365ModuleStatus

Disconnect-M365 (alias dm365) safely disconnects from all active services without errors if any session is not currently connected. Get-M365ModuleStatus (alias gm365) displays a table of all modules, their installation state and current version — useful for quickly verifying the environment or troubleshooting issues.

⚠️
The Connect-MgGraph scopes are intentionally broad. The Connect-M365 function requests Directory.ReadWrite.All and Sites.FullControl.All, which are appropriate for a consultant toolkit with full tenant access. In automation scripts or environments where least privilege applies, reduce the scopes to the minimum required for the task at hand.

How to run the script

  1. Download the script — save Install-M365Modules.ps1 to a local folder, for example C:\Scripts\.
  2. Open PowerShell as Administrator — right-click PowerShell and select "Run as administrator". If you use Windows Terminal, open an elevated tab. Installing to AllUsers requires admin privileges.
  3. Navigate to the script foldercd C:\Scripts
  4. Run the script — if the execution policy has not been configured yet, use powershell -ExecutionPolicy Bypass -File .\Install-M365Modules.ps1. The script sets the execution policy automatically during the run.
  5. Answer the two interactive prompts — whether to remove legacy modules (Y/N) and whether to create the profile with the Connect-M365 functions (Y/N). Both default to N if you press Enter without answering.
  6. Restart PowerShell — required for the profile to be loaded and for Connect-M365, dm365 and gm365 to become available.
PowerShell — Run with policy bypass
# Run from an elevated PowerShell session
Set-Location "C:\Scripts"
powershell -ExecutionPolicy Bypass -File .\Install-M365Modules.ps1

# Or, if the execution policy is already configured
.\Install-M365Modules.ps1

Script output

The script uses a status bar with colour-coded prefixes for each action — [OK] in green, [WARN] in yellow, [ERROR] in red, [SKIP] in grey. At the end, it prints a tabular summary with the status and version of each module.

Sample output — final summary
======================================================================
  5. INSTALLATION SUMMARY
======================================================================

Module                                          Status        Version
------                                          ------        -------
Microsoft.Graph                                 Installed     2.26.1
ExchangeOnlineManagement                        Installed     3.7.1
Microsoft.Online.SharePoint.PowerShell          Installed     16.0.25514.12000
PnP.PowerShell                                  Installed     2.12.0
MicrosoftTeams                                  Installed     6.9.0
Microsoft.PowerApps.Administration.PowerShell   Installed     2.0.201
Microsoft.PowerApps.PowerShell                  Installed     1.0.44
ORCA                                            Installed     3.1.1
Microsoft365DSC                                 Installed     1.25.416.1
MSCommerce                                      Up to date    0.9

  Installed: 9 | Updated: 0 | Already current: 1 | Errors: 0

  NEXT STEP: To connect to all M365 services, run:
    Connect-M365 -UPN admin@yourdomain.com

Setup checklist

  • PowerShell 7.x installed Download from github.com/PowerShell/PowerShell. The script works with 5.1, but 7.x is recommended for the Graph SDK.
  • Script run as Administrator Ensures installation to AllUsers — all users on the machine have access to the modules.
  • Legacy modules removed (if applicable) AzureAD, AzureADPreview and MSOnline removed if existing scripts have already been migrated to the Graph SDK.
  • PowerShell profile created with Connect-M365 Answer Y when the script prompts. The profile is created at $PROFILE.CurrentUserAllHosts and is loaded automatically in every new session.
  • PowerShell restarted Required to load the profile and activate Connect-M365, Disconnect-M365 and Get-M365ModuleStatus.
  • Environment verified with gm365 Run Get-M365ModuleStatus (or gm365) to confirm all modules are installed at the correct version.

Download
Install-M365Modules.ps1
Related article
Audit Conditional Access Exclusions with PowerShell


Next
Next

Audit Conditional Access Exclusions with PowerShell