Using Out-GridView to simplify selecting the region when managing Microsoft Azure with PowerShell

You’ve signed up for a Microsoft Azure account and you’ve installed the Azure Resource Manager PowerShell cmdlets on your computer.

Install-Module -Name AzureRM -Force

azure-region1a.jpg

You login to Azure from PowerShell. You’ll normally see most people use Login-AzureRmAccount, but that command is an alias (Login isn’t an approved verb).

Get-Alias -Definition Add-AzureRmAccount

azure-region2a.jpg

Login to Azure and provide the account login information when prompted:

Add-AzureRmAccount

azure-region3a.jpg

Several of the cmdlets in the Azure Resource Manager PowerShell module require a location (a region) to be specified when creating things. To me, showing the regions in the PowerShell console, figuring out which region I want, and then storing it in a variable is a bit haphazard.

Get-AzureRmLocation

azure-region4a.jpg

I’ve found that using Out-GridView makes the process simpler:

$Region = (
    Get-AzureRmLocation |
    Sort-Object -Property Location |
    Select-Object -Property Location, Displayname |
    Out-GridView -OutputMode Single -Title 'Select an Azure Region'
).Location

azure-region5a.jpg

Now the region that I selected is stored in a variable that I can use when creating items in Azure:

$Region

azure-region6a.jpg

Although the Out-GridView cmdlet existed in PowerShell version 2.0, you’ll need PowerShell version 3.0 to use it as shown in this blog article. The OutputMode parameter of Out-GridView which is used in this blog article was added in PowerShell version 3.0. Also, Out-GridView can only be used on operating systems with a GUI (it cannot be used on server core).

As far as I know, there’s no way to set a default region in Azure like there is with the AWS Initialize-AWSDefaultConfiguration cmdlet. I guess if you really wanted to set a default, you could always use $PSDefaultParameterValues to set a default value for the Location parameter for the cmdlets that require it.

ยต