Import-AzContext

Imports an Azure PowerShell context from a file and makes it the current active context for the session.

Syntax

Import-AzContext
   -Path <String>
   [-Force]
   [-Scope <AzContextScope>]
   [-DefaultProfile <IAzContext>]
   [CommonParameters]

Parameters

  • -Path String – The full path to the context file. This parameter is required.
  • -Force – Overwrites the current context without prompting.
  • -Scope AzContextScope – Specifies the scope of the imported context (CurrentUser, Process, etc.). Default is Process.
  • -DefaultProfile IAzContext – An existing AzContext object to merge with the imported data.

Examples

Example 1: Import a context file

# Import the context stored in C:\AzContexts\MyContext.json
Import-AzContext -Path "C:\AzContexts\MyContext.json"

Example 2: Import and set as the default for the current user

# Import and persist the context for the current user
Import-AzContext -Path "C:\AzContexts\MyContext.json" -Scope CurrentUser

Example 3: Overwrite the current context without confirmation

# Force import, discarding any existing context
Import-AzContext -Path "C:\AzContexts\MyContext.json" -Force

Remarks

The context file is typically created using Export-AzContext. It contains authentication tokens, subscription information, and tenant details. When importing, ensure the file is protected because it may contain sensitive credentials.