Azure PowerShell Reference

Out-AzContext

The Out-AzContext cmdlet is used to write the current Azure context information to a file. This is useful for saving and restoring your current Azure session state.

Syntax


Out-AzContext
    [-File] <String>
    [-Force]
    [-Scope] <ContextScope>
    [-WhatIf]
    [-Confirm]

Out-AzContext
    -InputObject <AzContext>
    [-File] <String>
    [-Force]
    [-Scope] <ContextScope>
    [-WhatIf]
    [-Confirm]
                

Description

The Out-AzContext cmdlet allows you to export your current Azure context, including subscriptions, tenants, and logged-in accounts, to a specified file. This file can later be used with Import-AzContext to restore the exact same session. This is particularly helpful when working across different environments or when you need to share your context with others (though be mindful of security implications).

Parameters

Parameter Description Required
-File Specifies the path to the file where the context will be saved. The file will be created if it doesn't exist. Yes
-InputObject Specifies the Azure context object to export. If not provided, the current active context will be used. No
-Force Overwrites the file if it already exists without prompting for confirmation. No
-Scope Specifies the scope of the context to export. Valid values are CurrentUser (default) and Process. No
-WhatIf Shows what would happen if the cmdlet runs. The cmdlet is not run. No
-Confirm Prompts you for confirmation before running the cmdlet. No

Examples

Save the current context to a file


Out-AzContext -File "C:\Azure\MyContext.json"
                    

This command saves the current Azure context to a JSON file named MyContext.json in the specified directory.

Save the current context, overwriting if the file exists


Out-AzContext -File "C:\Azure\MyContext.json" -Force
                    

The -Force parameter ensures that the file is overwritten if it already exists.

Save a specific context object to a file


$context = Get-AzContext
Out-AzContext -InputObject $context -File "C:\Azure\SpecificContext.json"
                    

This example first retrieves the current context using Get-AzContext and then exports that specific context object to a file.

Related Cmdlets