Remove-AzContext

Removes a subscription context from the current Azure PowerShell session.

SYNOPSIS

Removes a subscription context from the current Azure PowerShell session.

SYNTAX

Remove-AzContext [-Name] <String> [-Force] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-AzContext [-Context] <PSAzureContext> [-Force] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

The Remove-AzContext cmdlet removes a subscription context from the current Azure PowerShell session.

An Azure PowerShell context contains information about the current user, subscription, and default Azure environment.

This cmdlet is useful for cleaning up your session when you have multiple contexts loaded and want to remove one that is no longer needed.

PARAMETERS

Name Description Required?
-Name Specifies the name of the context to remove. You can obtain context names by using Get-AzContext. True (unless -Context is used)
-Context Specifies the context object to remove. You can obtain a context object by using Get-AzContext. True (unless -Name is used)
-Force Suppresses confirmation prompts for removing the context. False
-PassThru Returns the context object that the cmdlet removed. By default, this cmdlet does not output any objects. False
-WhatIf Shows what would happen if the cmdlet runs. The cmdlet is not run. False
-Confirm Prompts you for confirmation before running the cmdlet. False

EXAMPLES

Example 1: Remove a context by name
Remove-AzContext -Name "MyDevelopmentSubscription"

This command removes the Azure context named "MyDevelopmentSubscription" from the current session.

Example 2: Remove the current context
Get-AzContext | Remove-AzContext

This command gets the current Azure context and pipes it to Remove-AzContext to remove it.

Example 3: Remove a context and output the removed context
Remove-AzContext -Name "OldSubscription" -PassThru

This command removes the context named "OldSubscription" and returns the context object to the pipeline.

Example 4: Remove a context without confirmation
Remove-AzContext -Name "TemporaryContext" -Force

This command removes the context named "TemporaryContext" without prompting for confirmation.

INPUTS

Microsoft.Azure.Commands.Common.Models.PSAzureContext

You can pipe a context object to this cmdlet.

OUTPUTS

Microsoft.Azure.Commands.Common.Models.PSAzureContext

When the -PassThru parameter is used, this cmdlet returns the context object that was removed.

NOTES

Important: Removing the currently active context will require you to re-authenticate or set a new default context for subsequent operations.
Use Get-AzContext to view the contexts available in your session.

RELATED LINKS