Table of Contents

Disconnect-PnPOnline

SYNOPSIS

Disconnects the current connection and clears its token cache.

SYNTAX

Disconnect-PnPOnline [-ClearPersistedLogin]

DESCRIPTION

Disconnects the current connection and optionally clears its token cache. It will require you to build up a new connection again using Connect-PnPOnline in order to use any of the PnP PowerShell cmdlets. You might have to reauthenticate. If instead you simply want to connect to another site collection within the same tenant using the same credentials you used previously, do not use this cmdlet but instead use Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/othersite instead without disconnecting. It will try to reuse the existing authentication method and cached credentials.

Note that this cmdlet does not support passing in a specific connection to disconnect. If you wish to dispose a specific connection you have set up in a variable using $variable = Connect-PnPOnline -ReturnConnection, just dispose that variable using $variable = $null and it will be cleared from memory.

EXAMPLES

EXAMPLE 1

Disconnect-PnPOnline

This will clear out all active tokens from the current connection

EXAMPLE 2

Disconnect-PnPOnline -ClearPersistedLogin

This disconnects and removes the persisted login that was registered with Connect-PnPOnline -PersistLogin for the tenant and client ID of the current connection, so the next connection has to authenticate again.

PARAMETERS

-ClearPersistedLogin

Removes the persisted login registered with Connect-PnPOnline -PersistLogin for the tenant and client ID of the current connection, and clears its local token cache. For a delegated connection this attempts to remove the account from the shared token cache. For a certificate-based app-only connection this deletes the cache dedicated to that tenant and client ID from secure storage. A warning is written when no persisted login is registered for the current connection, for both delegated and app-only authentication; use Get-PnPPersistedLogin to see which ones are registered.

If app-only token deletion fails, for example because the cache file is locked or secure storage is unavailable, the cmdlet reports an error and retains the connection and registration so cleanup can be retried. This also applies when the connection was established without persistence after a secure-storage warning. Restore access to secure storage and run Disconnect-PnPOnline -ClearPersistedLogin again.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

Persisted Login

Microsoft 365 Patterns and Practices