Live training: next batches coming soon. Join the waitlist:Waitlists open:Developer Training →Developer →Copilot Studio Training →Copilot Studio →

How to Connect to SharePoint Online Using PowerShell (PnP and SPO Management Shell)

A client recently asked me for a report of every document library on 40 intranet sites. Clicking through Site contents wasn’t an option, so step one was a PowerShell session connected to SharePoint Online. That step trips people up now, because older copy-paste commands stop at an error.

Here’s how I connect today, tested on my own Microsoft 365 tenant in PowerShell 7: PnP PowerShell for site work, the SharePoint Online Management Shell for tenant admin tasks, certificates for unattended scripts, and fixes for the usual errors.

Quick answer: For a site, install PnP PowerShell in PowerShell 7.4+, register your own Entra ID app once with Register-PnPEntraIDAppForInteractiveLogin, then run Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/Marketing -ClientId <your-app-id> -Interactive. For tenant administration, install Microsoft.Online.SharePoint.PowerShell and run Connect-SPOService -Url https://contoso-admin.sharepoint.com as a SharePoint Administrator.

PnP PowerShell or SharePoint Online Management Shell?

There are two PowerShell modules for SharePoint Online, and they connect to different things. Pick based on the job, not habit. If you’re new to SharePoint itself, the SharePoint tutorials hub covers the basics first.

PnP PowerShellSharePoint Online Management Shell
Connect cmdletConnect-PnPOnlineConnect-SPOService
Connects toOne site (any URL)The SharePoint admin center
Who can use itAnyone with access to the siteSharePoint Administrators
PowerShell version7.4.0 or laterWindows PowerShell 5.1, or 7 with -UseWindowsPowerShell
Typical jobsLists, items, libraries, files, pages, permissionsCreate or delete sites, storage quotas, sharing settings, tenant reports

Install the modules and check your PowerShell version

PnP PowerShell 3.x requires PowerShell 7.4.0 or later, so it won’t load in the blue Windows PowerShell 5.1 console. Install PowerShell 7 first if you don’t have it, then install the modules for your account. I walk through every install option in installing PnP PowerShell for SharePoint Online.

# PnP PowerShell (needs PowerShell 7.4 or later)
Install-Module PnP.PowerShell -Scope CurrentUser
Update-Module PnP.PowerShell

# SharePoint Online Management Shell
Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser
Update-Module Microsoft.Online.SharePoint.PowerShell

-Scope CurrentUser skips the need for an elevated window. Here’s how I confirm the PowerShell version and the newest version of each module:

Check PowerShell 7 and PnP PowerShell module versions before connecting to SharePoint Online

Register your own Entra ID app for PnP PowerShell

This is the step most older guides skip. Since September 9, 2024, Connect-PnPOnline needs a -ClientId for interactive, device code and credential sign-ins. The shared PnP Management Shell app that used to work without one is gone, so you register an app in your own tenant once and reuse its ID.

The quickest way is the PnP cmdlet, which creates the app with a limited set of delegated permissions and asks you to consent:

Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP.PowerShell" -Tenant contoso.onmicrosoft.com
  • Role: Application Developer can create the app, but granting admin consent usually needs a Global Administrator. If the Grant admin consent button is grayed out, ask your admin.
  • Client ID: copy the Application (client) ID from the output or from Microsoft Entra admin center > App registrations. That GUID is your -ClientId.
  • Manual registration: if you create the app yourself, add http://localhost (http, not https) as a Mobile and desktop applications redirect URI.

Tired of pasting the GUID? Set the ENTRAID_APP_ID environment variable in your profile and PnP picks it up automatically.

Connect to a SharePoint site with Connect-PnPOnline

With the app in place, connecting to a site is one line. -Interactive opens the Microsoft sign-in window, so MFA and Conditional Access work the way they do in the browser:

$siteUrl  = "https://contoso.sharepoint.com/sites/Marketing"
$clientId = "00000000-0000-0000-0000-000000000000"   # your app's Application (client) ID

Connect-PnPOnline -Url $siteUrl -ClientId $clientId -Interactive

On a server without a browser, or over a remote session, use -DeviceLogin instead. PnP shows a code; you open microsoft.com/devicelogin on any device, enter it and sign in. Add -PersistLogin and PnP caches the token, so the next session for the same tenant and client ID connects without a prompt.

Here’s the script I ran against my test site, using device login with a persisted token:

Connect-PnPOnline connecting to a SharePoint Online site in PowerShell 7 and Get-PnPWeb output

Check which site and app your session is using

Before running anything that writes data, I confirm where I’m connected. Get-PnPConnection shows the site URL, client ID and sign-in method, and a quick read like Get-PnPList proves your account can actually see the site’s content:

Get-PnPConnection and Get-PnPList output verifying the SharePoint Online PowerShell connection

Once that works, you’re ready for real tasks such as list item CRUD operations with PnP PowerShell or reading list column details and internal names.

Connect without a sign-in: app-only with a certificate

Scheduled tasks and Azure Automation runbooks can’t click through a sign-in window. For those, register an app with a certificate (Register-PnPEntraIDApp -ApplicationName "PnP Automation" -Tenant contoso.onmicrosoft.com -OutPath C:\Certs creates the .cer and .pfx files), grant it SharePoint application permissions with admin consent, and connect with the certificate:

$pfxPassword = Read-Host -Prompt "Certificate password" -AsSecureString

Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/Finance" `
    -ClientId "00000000-0000-0000-0000-000000000000" `
    -Tenant "contoso.onmicrosoft.com" `
    -CertificatePath "C:\Certs\PnP-Automation.pfx" `
    -CertificatePassword $pfxPassword

If the certificate is in the Windows certificate store, use -Thumbprint instead of the file. App-only connections act as the app, not as you, so grant only the permissions the script needs. In Azure Automation, a managed identity (-ManagedIdentity) avoids certificates entirely. For ready-made scripts, see my PnP PowerShell scripts for SharePoint developers.

Connect to the SharePoint admin center with Connect-SPOService

For tenant-wide work, connect to the admin center URL, which is your tenant name followed by -admin. You need the SharePoint Administrator (or Global Administrator) role. Running this without -Credential shows the sign-in window, which is the method to use when your account has MFA:

# PowerShell 7 only: load the module through Windows PowerShell compatibility
Import-Module Microsoft.Online.SharePoint.PowerShell -UseWindowsPowerShell

Connect-SPOService -Url https://contoso-admin.sharepoint.com
Get-SPOSite -Limit 5 | Select-Object Url, Owner, StorageUsageCurrent

Only one Connect-SPOService connection exists per session; running it again replaces the old one. For unattended admin scripts, it also accepts -ClientId, -TenantId and -CertificateThumbprint. GCC High and DoD tenants add -Region ITAR.

Troubleshooting common connection errors

This is what you’ll see if you run a PnP cmdlet before connecting, or after the session has ended:

PnP PowerShell error You are not signed in when Get-PnPWeb runs before Connect-PnPOnline
  • “You are not signed in. Please use Connect-PnPOnline to connect.” Each PowerShell window has its own session. Connect again in this window, or use -PersistLogin so reconnecting is silent.
  • Connect-PnPOnline asks for a ClientId, or AADSTS700016 says the app isn’t found: you’re running an old command without -ClientId, or the ID belongs to another tenant. Register your own app as shown above.
  • AADSTS65001 (consent required): the app’s permissions weren’t consented. A Global Administrator needs to grant admin consent on the app registration.
  • AADSTS500113 (no reply address): the app registration has no redirect URI. Follow these steps to fix the AADSTS500113 no reply address error by adding a Mobile and desktop redirect URI.
  • AADSTS650057 (invalid resource): the app registration has no permission for the API PnP asked for, usually SharePoint. Here’s how to fix the AADSTS650057 resource not listed error by adding SharePoint permissions and granting admin consent.
  • “Current site is not a tenant administration site”: Connect-SPOService got a site URL. Use https://contoso-admin.sharepoint.com instead.
  • “Connect-SPOService is not recognized”: the module isn’t installed or loaded. In PowerShell 7, import it with -UseWindowsPowerShell. For a 403 response, see fixing the Connect-SPOService 403 Forbidden error.
  • “Could not connect to SharePoint Online”: add -ModernAuth $true -AuthenticationUrl https://login.microsoftonline.com/organizations to Connect-SPOService. Older CSOM scripts fail inside ExecuteQuery instead; see the ExecuteQuery unable to connect error.

Disconnect and clear saved sign-ins

On a shared admin machine, I disconnect on purpose, especially after -PersistLogin. Get-PnPPersistedLogin lists what’s cached:

Disconnect-PnPOnline -ClearPersistedLogin  # sign out and remove the cached token
Disconnect-SPOService                      # end the admin center session

Once you’re connected, try a script on a test list, such as deleting all items from a SharePoint list using PnP PowerShell.

Once your connection works, these PnP PowerShell guides are good next steps:

Frequently Asked Questions

How do I connect to SharePoint Online using PowerShell?

Use Connect-PnPOnline with your site URL, your own app’s -ClientId and -Interactive for site work, or Connect-SPOService with your admin center URL for tenant administration.

Why does Connect-PnPOnline ask for a client ID?

Since September 9, 2024, interactive, device code and credential sign-ins need your own Entra ID app registration because the shared PnP app no longer works. The Connect-PnPOnline reference lists every sign-in option.

Does Connect-SPOService work with MFA?

Yes. Run Connect-SPOService -Url https://contoso-admin.sharepoint.com without -Credential, and the sign-in window handles MFA.

Can I use PnP PowerShell in Windows PowerShell 5.1?

No. Current PnP PowerShell needs PowerShell 7.4.0 or later. The SharePoint Online Management Shell still runs in Windows PowerShell 5.1.

How do I connect to SharePoint Online in PowerShell without a sign-in prompt?

Use app-only authentication: an Entra ID app with a certificate and SharePoint application permissions, then Connect-PnPOnline with -ClientId, -Tenant and -CertificatePath or -Thumbprint. Microsoft’s SharePoint Online Management Shell guide covers the admin module side.