Essential Azure PowerShell and CLI Commands (2024)

Essential Azure PowerShell and CLI Commands (2)

In this article, we will explore the main differences between Azure CLI and Azure PowerShell. We’ll examine their unique features, use cases, and benefits.

Whether you’re a developer, system administrator, or simply enthusiastic about cloud technology, this comprehensive comparison will equip you with the insights necessary to maximize your productivity and effectively manage Azure resources.

  1. What is the difference between Azure CLI and Azure PowerShell?
  2. Why should you consider using Azure CLI, Azure PowerShell (or both)
  3. Using Azure CLI and Azure Powershell from Azure Portal
  4. Using Azure Powershell From your computer
  5. PowerShell Reference commands

5.1 Accounts and Subscriptions

5.2 Resource Groups

5.3 Governance

5.4 Storage

5.5 Deploy and Manage Virtual Machines

5.6 Networking

5.7 Azure Active Directory Commands

Azure PowerShell and Azure CLI are both command-line interfaces for interacting with Microsoft Azure, each with its own characteristics.

Azure PowerShell, built on the PowerShell scripting language, is well-suited for Windows-centric environments and offers powerful scripting capabilities using a Verb-Noun syntax (e.g., Get-AzResource).

Azure Powershell supports interactive scripting, deep integration with the Windows ecosystem, and outputs objects in PowerShell format.

On the other hand, Azure CLI is a cross-platform tool that uses a shell scripting approach (bash, PowerShell) and outputs in various formats like JSON and tables.

It is known for its ease of use, quick command-line tasks, and broader ecosystem integration.

The choice between them often depends on factors such as platform preference, scripting language familiarity, and specific use cases.

Both tools provide similar functionality for managing Azure resources, allowing users to choose based on their preferences and requirements.

Here is a summary table of the differences between Azure PowerShell and Azure CLI:

Essential Azure PowerShell and CLI Commands (3)

There are several reasons why you might consider using Azure CLI or Azure PowerShell for managing your Azure resources:

  1. Automation: Both Azure CLI and Azure PowerShell provide powerful scripting capabilities, allowing you to automate repetitive tasks and streamline your workflows. By writing scripts, you can save time and ensure consistency in your Azure deployments and configurations.
  2. Flexibility: Azure CLI and Azure PowerShell offer a wide range of commands and modules that enable you to interact with various Azure services and resources. They provide comprehensive coverage of Azure functionalities, giving you the flexibility to manage and customize your Azure environment to meet your specific needs.
  3. Cross-platform compatibility: Azure CLI is designed to work on multiple platforms, including Windows, macOS, and Linux. This cross-platform support allows you to use Azure CLI regardless of your operating system, providing a consistent experience across different environments. Azure PowerShell, on the other hand, is primarily targeted for Windows users.
  4. Integration with other tools and workflows: Azure CLI and Azure PowerShell integrate well with other tools and systems. They can be easily incorporated into existing DevOps pipelines, configuration management frameworks, and continuous integration/continuous deployment (CI/CD) processes. This integration enables seamless collaboration and automation within your development and deployment workflows.

Ultimately, the choice between Azure CLI and Azure PowerShell depends on factors such as your familiarity with scripting languages, your preferred operating system, and the specific requirements of your Azure environment.

Additionally, you can use a combination of Azure CLI and Azure PowerShell when necessary to leverage their respective strengths and functionalities.

Azure CLI and Azure PowerShell can be accessed within the Azure cloud environment. Additionally, you have the flexibility to use both tools remotely from your local machine.

To access the Azure Cloud Shell, simply log in to the Azure Portal and locate the Cloud Shell icon.

Essential Azure PowerShell and CLI Commands (4)

Clicking on this icon will provide you with access to the Azure Cloud Shell.

Essential Azure PowerShell and CLI Commands (5)

You can switch from Powershell to Bash (Azure CLI)

In our example, wi still in powershell envirement, so we want to list our VM on our subscription :

Get-AzVM

In response, we have the list of all the virtual machines in our subscription.

Essential Azure PowerShell and CLI Commands (6)

If we switch to Azure Bash, and try to execute the same command:

Essential Azure PowerShell and CLI Commands (7)

To run the script from your Windows 11 computer, you need to first install the Az PowerShell module. Here are the steps to follow:

  1. Open PowerShell as an administrator. To do this, right-click on the PowerShell icon in the Start menu and select “Run as administrator.”
  2. In the PowerShell window, type the following command to install the Az PowerShell module:
Install-Module Az

Press Enter to execute the command. This will initiate the installation process of the Az PowerShell module.

Essential Azure PowerShell and CLI Commands (8)

Follow any prompts or confirmations that appear during the installation process (Like installing NuGet …)

Essential Azure PowerShell and CLI Commands (9)

Once the installation is complete, you can now run the script using the Az PowerShell module.

If you have written a script called “script.ps1” and stored it in C:, you simply need to navigate to the location of the script and then type the command :

.\script.ps1

The script will be executed, and you will be able to see the results in the PowerShell window.

Now, let’s take a look at the most important commands, and we will gradually add interesting commands along the way.

Feel free to leave your comments or important commands in the comments section.

5.1 Accounts and Subscriptions

Login to Azure Account:

Login-AzAccount

Upon entering this command, you will be redirected to
https://microsoft.com/devicelogin and presented with a popup
window to complete your login process and any MFA requirements.

Essential Azure PowerShell and CLI Commands (10)

Logout of the Azure account you are connected with in your session:

Logout-AzAccount 

List all subscriptions in all tenants the account can access:

Get-AzSubscription

Get subscriptions in a specific tenant:

Get-AzSubscription -TenantId "xxxx-xxxx-xxxxxxxx"

Choose subscription:

Select-AzSubscription –SubscriptionID
“SubscriptonID”

5.2 Resource Groups

Get all resource groups:

Get-AzResourceGroup

Get resource groups where the name begins with “morsi”:

Get-AzResourceGroup | Where ResourceGroupName -like
morsi*

Create a new Resource Group:

New-AzResourceGroup -Name 'My-RG' -Location 'northcentral'
#Creates a new resource group in North Central called “My-RG”

Moving Resources from One Resource Group to Another :

$Resource = Get-AzResource -ResourceType "Microsoft.ClassicCompute/storageAccounts" -
ResourceName "MyStorageAccount"
# Retrieves a storage account called “MyStorageAccount”

Move-AzResource -ResourceId
$Resource.ResourceId -DestinationResourceGroupName
"My-NewRG"
# Moves the resource from Step 1 into the destination resource group “My-NewRG”

5.3 Governance

Azure Policies: View Policies and Assignments

See all policy definitions in your subscription:

Get-AzPolicyDefinition

Retrieve assignments for a specific resource group:

$rg = Get-AzResourceGroup -Name "ExampleGroup"
Get-AzPolicyAssignment -Name accessTierAssignment -Scope $rg.ResourceId

5.4 Storage

Lists all storage accounts in the current subscription

Get-AzStorageAccount 

Create Storage Account.

Requires the resource group name, storage account name, valid Azure location, and type (SkuName).

New-AzStorageAccount -ResourceGroupName “mystoragerg” -Name “mystorage1” -Location
“eastus”-SkuName “Standard_LRS”

SKU Options:

  • Standard_LRS: Locally-redundant storage.
  • Standard_ZRS: Zone-redundant storage.
  • Standard_GRS: Geo-redundant storage.
  • Standard_RAGRS: Read access geo-redundant storage.
  • Premium_LRS: Premium locally-redundant storage.

5.5 Deploy and Manage Virtual Machines

List VMs in a resource group (See Resource Groups section above)

Get -AzVM -ResourceGroupName $myResourceGroup

Create a simple VM (Typing in this simple command will create a VM and populate names for all the associated objects based on the VM name specified)

New-AzVM -Name “myvm” 

Create a VM configuration:

$vmconfig = New-AzVMConfig -VMName “systemname” -VMSize "Standard_D1_v2"

Add configuration settings This adds the operating system settings to the configuration:

$vmconfig = Set-AzVMOperatingSystem -VM $vmconfig -Windows -
ComputerName “systemname” -Credential $cred -ProvisionVMAgent
EnableAutoUpdate

Add a network interface:

$vmconfig = Add-AzVMNetworkInterface -VM $vmconfig -Id $nic.Id

Specify a platform image:

$vmconfig = Set-AzVMSourceImage -VM $vmconfig -PublisherName
"publisher_name" -Offer "publisher_offer" -Skus "product_sku" -Version "latest"

Create a VM:

New-AzVM -ResourceGroupName “slresourcegroup” -Location “eastus”
-VM $vmconfigconfig

5.6 Networking

List virtual networks:

Get-AzVirtualNetwork -ResourceGroupName “slresourcegroup”

Get information about a virtual network:

Get-AzVirtualNetwork -Name "myVNet" -ResourceGroupName “slresourcegroup” 

5.7 Azure Active Directory Commands

In order to use the Azure AD commands, you first need to install the Azure AD module. Use the following procedure to get it installed:

  1. Open PowerShell
  2. Type “Install-Module AzureAD”
  3. Press Y to accept the untrusted repository (PSGallery)
Essential Azure PowerShell and CLI Commands (11)

Connect to Azure Active Directory:

Connect-AzureAD

Get all users:

Get-AzureADUser

Get specific user:

Get-AzureADUser -ObjectId "[email protected]"

New User Creation. This is a 3 step process that requires first creating a password profile, setting the password, and then passing these into the NewAzureADUser command.

#1. Create Password Profile
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile

#2. Set Password
$PasswordProfile.Password = "Password"

#3. Create User
New-AzureADUser -DisplayName "New User" -
PasswordProfile $PasswordProfile -UserPrincipalName -
"[email protected]" -AccountEnabled $true -
MailNickName "Newuser"

Assign Role:

New-AzRoleAssignment -ResourceGroupName
“myresourcegroup” -ObjectId 11111111-1111-1111-1111-
11111111111 -RoleDefinitionName Reader
Essential Azure PowerShell and CLI Commands (2024)
Top Articles
Where and How to Buy XRP Without Paying Fees | Lykke
Marine Corps Uniform Board
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 5630

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.