How to Install & Configure Azure PowerShell

What is PowerShell?

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.

Initially a windows component only known as Windows PowerShell, it was made open source and cross platform on 18th August with the introduction of PowerShell core.

How to use PowerShell?

  • Tasks are executed by command-lets (cmdlets).
  • Each command-let contains verb which describes action that will be taken
    1.   Get-process
    2.   join-path
    3.   Connect-PSSession
  • All PowerShell command-lets are parameterized to ensure maximum flexibility.

Azure PowerShell Review

Azure PowerShell is a set of cmdlets designed to help with management of Azure Resources directly from the PowerShell command line.

Most common scenarios are:

  • Day to day operation such as Automation of repititive tasks, Scheduling, deployment etc.
  • Devops
  • Learning
  • Achieving things that are hard otherwise

Installing & Configuring PowerShell Az Module 

There are two modules bundle available.

  • AzureRm (Azure Resource Management)
  • Az (new modules; recommended)

Let’s install and import Az module and connect to our Azure Portal.

Install-Module Az -Force -AllowClobber

Import-Module Az

Log into Azure

To connect to Azure, use the Connect-AzAccount cmdlet:

# Device Code login - It provides a link to sign into Azure via web browser

Connect-AzAccount

Azure Powershell

Now you will asked to provide your Azure Portal credentials. Please provide credentials and you are all set to work.

To log into a specific cloud such as (AzureChinaCloud, AzureCloud, AzureGermanCloud, AzureUSGovernment), use the -Environment parameter:

Connect-AzAccount -Environment AzureChinaCloud

Getting and setting Azure PowerShell Session Context

A session context persists login information across Azure PowerShell modules and PowerShell instances. To view the context you are using in your current session which contains the subscription and tenant then use the Get-AzContext cmdlet:

Get-AzContext

To lists all available Azure PowerShell contexts in the current PowerShell session, use below cmdlet;

Get-AzContext -ListAvailable

To get the subscriptions in a tenant, use the Get-AzSubscription cmdlet:

Get-AzSubscription

To get all of the Azure subscriptions in a specific Azure tenant then use below cmdlet:

Get-AzSubscription -TenantId  $TenantId

To change the subscription that you are using for your current context, use the Set-AzContext cmdlet. You can set the Azure PowerShell context to a specific Azure subscription by below commands:

Set-AzContext -Subscription $SubscriptionName -Name 'MyContext'

You can set the Azure PowerShell context using piping

Get-AzSubscription -SubscriptionName $SubscriptionName | Set-AzContext -Name 'MyContext'

Discovering cmdlets

Use the Get-Command cmdlet to discover cmdlets within a specific module, or cmdlets that follow a specific search pattern: You can list all cmdlets in the Az.Accounts module

Get-Command -Module Az.Accounts

Get-Command -Name '*VirtualNetwork*'

Get-Command -Module Az.Compute -Name '*VM*'

Cmdlet help and examples

Help content can be viewed for a cmdlet by using the Get-Help cmdlet:

Get-Help -Name Get-AzSubscription

Get-Help -Name Get-AzSubscription -Examples

Get-Help -Name Get-AzSubscription -Full

Get-Help -Name Get-AzSubscription -Online

Read More: Microsoft Workflow – Getting started with Power Automate

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment