9 Most Popular Azure Powershell Examples


In this article, i will take you through 9 Most Popular Azure Powershell Examples. Microsoft Azure (formerly Windows Azure) is a cloud computing service provided by Microsoft. Azure provides the platform and infrastructure to deploy and manage applications and services through a global network of datacenters.

9 Most Popular Azure Powershell Examples 2

Azure Powershell Examples

Also Read: Create Virtual Machine in Azure with Just 7 Easy Steps


1. Install Azure Powershell Modules

First you need to install azure Powershell modules using below command to connect Microsoft Azure.

PS C:\> Install-Module -Name Az -AllowClobber -Scope AllUsers
Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A

2. Connect Microsoft Azure

Once Azure module is installed through azure powershell command, you can connect microsoft azure Cloud by using Connect-AzureRMAccount command as shown below.

PS C:\> Connect-AzureRMAccount
Account                   SubscriptionName                        TenantId               Environment
-------                   ----------------                        --------               -----------      Microsoft Azure Enterprise   9187989-8c5e-41d4-82e8-3d1bfjh67b2f   AzureCloud

3. Get Azure Tenant

If you want to check all the tenant authorized for the current logged in user, you can check it by using Azure powershell Get-AzureRMTenant command as shown below.

PS C:\> Get-AzureRMTenant
Id                                    Directory
--                                    ---------

4. Disconnect Azure Account

If you want to disconnect from Microsoft Azure account, you can disconnect it by using Disconnect-AzureRMAccount command.

PS C:\> Disconnect-AzureRMAccount

Id :
Type : User
Tenants : {91d27ab9-8c5e-41d4-82e8-3d1bf81fcb2f}
AccessToken :
Credential :
TenantMap : {}
CertificateThumbprint :
ExtendedProperties : {[Subscriptions, 69cbf2fc-73ff-4ad1-80b4-0e83f418491d], [Tenants, 91d27ab9-8c5e-41d4-82e8-3d1bf81fcb2f]}

5. List all the Resources in Current Subscription

If you want to List all the resources in the current subscription, then you need to use Get-AzureRmResource Azure Powershell command to list it as shown below.

PS C:\> Get-AzureRmResource | ft

Name                                                                ResourceGroupName
----                                                                -----------------
azureOsDisk_1_840e9f588eab424d93364145afc65ccf                          TestMP
Instance-DataDisk_0                                                     TestMP
Instance-OsDisk_1_fa64d020239yhge8b5c648f765ec4c8e                      TestMP
instance_disk1_c42cbefc4965487jh9e3b319bca033dc                         TestMP
example-disk1_1cd093db7adcnk7jfa9f2f2be4770e73                          TestMP
test0_DataDisk_0                                                        TestMP
test0_OsDisk_1_c8d7d43ea7f243a9ui89j48b66998b3e                         TestMP
test1_DataDisk_0                                                        TestMP
test1_OsDisk_1_2925e1774ce045b6b6cad9bdaaf91691                         TestMP
test2_DataDisk_0                                                        TestMP
test2_OsDisk_1_f81916f00ea646b5ab6cf27c8955cef0                         TestMP
test_disk1_02f6bc0e047148c7ba52063f26216ad2                             TestMP
OsDisk_1_445173ba53dc4e8jf5430d272b41e007                               TestMP
Instance_OsDisk_1_099b948d43e4421c9af52d3f6d022bf4                      TestMP
Instance_OsDisk_1_fa28d97c9jhg675hd4e7ebe289a164a0                      TestMP
image                                                                   TestMP

6. List all resources in a Resource Group

If you want to list all the resources of a particular Resource Group, then you can use -ResourceGroupName option with Get-AzureRMResource command to check that.

PS C:\> Get-AzureRMResource -ResourceGroupName azureMP | ft

Name                                                ResourceGroupName   ResourceType
----                                                -----------------   ------------
test11-inyOsDisk_1_840e9f588eab424d93364145afc65ccf     TestMP         Microsoft.Compute/disks
example-test-98714hgkjDataDisk_0                        TestMP         Microsoft.Compute/disks
test0989_hjhyt_disk_exOsDisk_1_fa64d020239b4be8b        TestMP         Microsoft.Compute/disks
instance_disk1_c42cbejhuy76fg54rfc4965443b89e3b         TestMP         Microsoft.Compute/disks
disk1_1cd093db7aklo0983ddc40fb9a9f2f2be4770e73          TestMP         Microsoft.Compute/disks
test0_DataDisk_0                                        TestMP         Microsoft.Compute/disks
test0_Oskjsk_1_c8d7d43ea7f243a9a00c748b66998b3e         TestMP         Microsoft.Compute/disks
test1_DjhlDisk_0                                        TestMP         Microsoft.Compute/disks
test1_Os78sk_1_2925e1774cjhukjufread9bdaaf91691         TestMP         Microsoft.Compute/disks
test2_oa98Disk_0                                        TestMP         Microsoft.Compute/disks
test2_psDisk_1_f81916f00eklkjjhtydcf27c8955cef0         TestMP         Microsoft.Compute/disks
test-908765th_disk1_02fkjhjiutyujkh8c7ba52063f26216ad2  TestMP         Microsoft.Compute/disks

7. List all the Resource Providers

If you want to list all the resource providers, then you need to use Get-AzureRMResourceProvider command as shown below.

PS C:\> Get-AzureRMResourceProvider | ft

ProviderNamespace              RegistrationState                        ResourceTypes
-----------------              -----------------                        -------------
Microsoft.Security                 Registered           {operations, securityStatuses, tasks, regulatoryComplianceStandard...
Microsoft.Compute                  Registered           {availabilitySets, virtualMachines, virtualMachines/extensions, vi...
Microsoft.Network                  Registered           {virtualNetworks, publicIPAddresses, networkInterfaces, privateEnd...
Microsoft.Storage                  Registered           {storageAccounts, operations, locations/asyncoperations, storageAc...
Microsoft.ResourceHealth           Registered           {availabilityStatuses, childAvailabilityStatuses, childResources, ...
Microsoft.PolicyInsights           Registered           {policyEvents, policyStates, operations, asyncOperationResults...}
Microsoft.ManagedIdentity          Registered           {Identities, userAssignedIdentities, operations}

8. List all Azure Locations

If you want to list all the Microsoft Azure Locations, then you can use Get-AzureRMLocation command and list it as shown below.

PS C:\> Get-AzureRMLocation | ft

Location       DisplayName       Providers
--------       -----------       ---------
eastasia       East Asia        {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
southeastasia  Southeast Asia   {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
centralus      Central US       {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
eastus         East US          {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
eastus2        East US 2        {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
westus         West US          {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
northcentralus North Central US {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
southcentralus South Central US {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...
northeurope    North Europe     {Microsoft.Security, Microsoft.Compute, Microsoft.Network, Microsoft.Storage...

9. List Azure Role Definition

If you want to list all the Azure role definitions, then you need to use Azure Powershell Get-AzureRMRoleDefinition command as shown below.

PS C:\> Get-AzureRMRoleDefinition | ft

Name                                                    Id                      IsCustom        Description
----                                                    --                      --------        -----------
AcrPush                                    8311e382-0749-4cb8-b61a-304f252e45ec False           acr push
API Management Service Contributor         312a565d-c81f-4fd8-895a-4e21e48d571c False           Can manage servic...
AcrPull                                    7f951dda-4ed3-4680-a7ca-43fe172d538d False           acr pull
AcrImageSigner                             6cef56e8-d556-48e5-a04f-b8e64114680f False           acr image signer
AcrDelete                                  c2f4ef07-c644-48eb-af81-4b1b4947fb11 False           acr delete
AcrQuarantineReader                        cdda3590-29a3-44f6-95f2-9f980659eb04 False           acr quarantine da...
AcrQuarantineWriter                        c8d4ff99-41c3-41a8-9f60-21dfdad59608 False           acr quarantine da...
API Management Service Operator Role       e022efe7-f5ba-4159-bbe4-b44f577e9b61 False           Can manage servic...
API Management Service Reader Role         71522526-b88f-4d52-b57f-d31fc3546d0d False           Read-only access ...
Application Insights Component Contributor ae349356-3a1b-4a5e-921d-050484c6347e False           Can manage Applic...
Application Insights Snapshot Debugger     08954f03-6346-4c2e-81c0-ec3a5cfae23b False           Gives user permis...


Also Read: Automating Microsoft Azure with Powershell

Leave a Comment