- Manage Azure PowerShell global settings - Fri, Sep 22 2023
- Create and manage append blobs with PowerShell - Wed, Oct 12 2022
- Permanently delete a Key Vault in Azure using PowerShell - Fri, Feb 4 2022
Before the release of Az PowerShell module version 9, Azure PowerShell had no centralized or fine-grained control over its global settings. Fortunately, starting with Az PowerShell module version 9, Azure PowerShell now has a useful set of cmdlets for managing these global settings, such as configuring default subscriptions, enabling or disabling data collection, and more.
Below are the supported cmdlets of the Az.Account module, which comes with the Az module by default.
- Get-AzConfig: Get the configs of Azure PowerShell
- Update-AzConfig: Update the configs of Azure PowerShell
- Export-AzConfig: Export all the configs into a JSON file
- Import-AzConfig: Import configs from a JSON file
- Clear-AzConfig: Clear the values of configs that are set by the user
With Get-AzConfig, Update-AzConfig, and Clear-AzConfig, the following settings are displayed when the cmdlet is run:
DefaultSubscriptionForLogin: When the Connect-AzAccount cmdlet is executed, the subscription specified in the default settings will automatically be selected as the active subscription. This feature allows us to minimize the risk of accidentally executing commands against the wrong subscriptions. This is particularly crucial in complex environments where multiple subscriptions are used, including those allocated for production use. By setting up a default subscription in the global settings, you create an additional layer to ensure that the cmdlets are run on the correct subscriptions.
DisplayBreakingChangeWarning: This setting controls the visibility of warning messages related to upcoming breaking changes in cmdlets. When enabled, it displays notifications in yellow if you execute cmdlets set for breaking changes in the upcoming releases.
DisplayRegionIdentified: When activated, this setting gives recommendations based on the regions that could potentially lower your expenses.
DisplaySurveyMessage: This setting is used for enabling/disabling prompts inviting you to participate in user experience surveys for Azure PowerShell.
EnableDataCollection: When this setting is enabled, Azure PowerShell cmdlets will automatically send telemetry data to Microsoft in the background.
EnableLoginByWam: This feature, currently in preview, sets the Web Account Manager (WAM) as the default method for interactive login. If the platform lacks WAM support, the system will automatically revert to browser-based authentication.
Viewing current settings with Get-AzConfig
The Get-AzConfig cmdlet is used to retrieve current Azure PowerShell global settings.
This command will output a list of all global settings across all scopes.
By default, the scope is set to Default, but if custom scopes have been defined, the configuration for each scope will be listed separately. For example, to retrieve the configuration for the setting DisplayBreakingChangeWarning, the command below can be used. If there is no custom setting, only the default configuration is listed.
If there is more than one configuration, all configurations for the same setting are listed.
Modifying settings with Update-AzConfig
Update-AzConfig is used to modify an existing configuration. This cmdlet provides some useful parameters to make the change for specific Az modules. It also gives us the ability to define a specific scope for applying settings, ensuring that the changes impact only targeted scopes, such as the current user, process, or environment.
For example, to modify the DisplayBreakingChangeWarning setting for the Azure Network module to ensure that the change applies across all sessions initiated by the current user, the following command can be used:
Update-AzConfig -DisplayBreakingChangeWarning $true -AppliesTo Az.Network -Scope CurrentUser
In the following example, the default subscription will be set so that the subscription defined in the config will be selected automatically when the Connect-AzAccount cmdlet is executed.
Update-AzConfig -DefaultSubscriptionForLogin "Lab"
This command sets the default subscription for Azure PowerShell when logging in to the specified subscription, here named Lab. In the above example, the current context is on a different subscription named 2023. When the Connect-AzConnect cmdlet is executed, the default subscription in the context is automatically switched back to the subscription named Lab, as we already set it as the default subscription.
Clear custom settings using Clear-AzConfig
Any custom settings created with Update-AzConfig can be deleted using Clear-AzConfig.
Clear-AzConfig -DisplayBreakingChangeWarning -Scope CurrentUser -AppliesTo Az.Network
Back up settings using Export-AzConfig
The Export-AzConfig command is used to export global settings into a JSON file, which can be used for backup or sharing.
The following command exports all the configurations from the CurrentUser scope into the path given by the user.
Export-AzConfig -Path c:\configbackups\config.json
Once exported, a sample config file will look like the following:
Restore or import settings using Import-AzConfig
Using Import-AzConfig, you can import global settings from a backup JSON file.
Import-AzConfig -Path c:\configbackups\config.json
In conclusion, managing Azure PowerShell global settings is a crucial aspect of Azure administration. With the help of the cmdlets Get-AzConfig, Update-AzConfig, Export-AzConfig, Import-AzConfig, and Clear-AzConfig, you can easily configure centralized and granular settings for your Azure PowerShell modules, including default subscriptions, region identification, and breaking change warning messages. If you want to learn more about configuring global settings for Azure PowerShell, you can refer to the Microsoft Learn documentation.