Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Set the Default Location in Azure PowerShell

May 23, 2024
in Blog, Microsoft Azure, Powershell
0
ADVERTISEMENT

Table of Contents

Set the Default Location in Azure PowerShell

This post explains how to set the default location for Azure PowerShell cmdlets, eliminating the need to specify it with each command.

PS C:\> New-AzResourceGroup -Name 'testRG1'

cmdlet New-AzResourceGroup at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Location:

The key to this is utilizing the PowerShell preference variable $PSDefaultParameterValues, which enables you to set a default value for cmdlets and parameters.

For instance, let’s create a Resource Groups in the East US region using New-AzResourceGroup, but without specifying the -Location parameter:

$PSDefaultParameterValues.Add("New-AzResourceGroup:Location","eastus")
New-AzResourceGroup -Name "testRG1"

The resource group is created in the East US region, as defined in our default.

YQiQwkEkMPzvRhYSg6ykzudvG9TT6G54MH25H4LOt04P6Tq193q9NMQWqwSv

This is beneficial, but it would require adding a $PSDefaultParameterValues.Add line for each cmdlet in the Azure PowerShell module that utilizes the Location parameter, such as New-AzVM, New-AzResourceGroup, and so on.

Fortunately, $PSDefaultParameterValues can accept wildcard entries, and due to the uniformity in PowerShell cmdlet and parameter naming, we can establish a default location for all cmdlets that include -Az in their name.

$PSDefaultParameterValues.Add("*-Az*:Location","eastus")

And then test with a variety of cmdlets:

$PSDefaultParameterValues.Add("*-Az*:Location","eastus")
New-AzVM -Name 'MyVm' -Credential (Get-Credential) -ResourceGroupName "testRG1"
New-AzAppServicePlan -ResourceGroupName "testRG1" -Tier Free -Name "test-asp-01"
PS C:\> Get-AzResource -ResourceGroupName 'testRG1' | ft

Name                                        ResourceGroupName ResourceType                            Location
----                                        ----------------- ------------                            --------
MyVm                                        testRG1           Microsoft.Network/virtualNetworks       eastus
MyVm                                        testRG1           Microsoft.Network/networkSecurityGroups eastus
MyVm                                        testRG1           Microsoft.Network/networkInterfaces     eastus
MyVm                                        testRG1           Microsoft.Compute/virtualMachines       eastus
MyVm_disk1_0b3e20900e5548ce9a39bd4cb45c225f TESTRG1           Microsoft.Compute/disks                 eastus
test-asp-01                                 testRG1           Microsoft.Web/serverFarms               eastus

It’s important to note that having this default setting does not affect Azure module cmdlets that do not need the location parameter; they will continue to function as expected.

Start-AzVM -Name 'MyVM' -ResourceGroupName "testRG1"

When a non-default Region is necessary, the location can be explicitly specified to override the default setting. For instance:

PS C:\> New-AzResourceGroup -Name "testRG2" -Location 'westus'

ResourceGroupName : testRG2
Location          : westus
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/2fe0cf1a-7f55-4c6e-850f-25136270db4f/resourceGroups/testRG2

If you typically work within the same region, you might consider setting this value in your PowerShell profile to ensure it’s the default for all sessions. Alternatively, you could create a brief function to invoke whenever you need to establish a new default, such as for a different client or project.

# Create a PowerShell function
function Set-AzDefaultLocation {
      param([string]$location)
      $PSDefaultParameterValues.Add("*-Az*:Location",$location)
}
Set-AzDefaultLocation -location 'eastus'
PS C:\> $PSDefaultParameterValues

Name                           Value
----                           -----
*-Az*:Location                 eastus
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Count the Number of Commands of an Installed PowerShell Module

Next Post

File Cannot be Loaded The File is Not Digitally Signed You Cannot Run this Script on The Current System

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory