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.
- What is the difference between Azure CLI and Azure PowerShell?
- Why should you consider using Azure CLI, Azure PowerShell (or both)
- Using Azure CLI and Azure Powershell from Azure Portal
- Using Azure Powershell From your computer
- 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:
There are several reasons why you might consider using Azure CLI or Azure PowerShell for managing your Azure resources:
- 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.
- 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.
- 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.
- 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.
Clicking on this icon will provide you with access to the Azure Cloud Shell.
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.
If we switch to Azure Bash, and try to execute the same command:
To run the script from your Windows 11 computer, you need to first install the Az PowerShell module. Here are the steps to follow:
- Open PowerShell as an administrator. To do this, right-click on the PowerShell icon in the Start menu and select “Run as administrator.”
- 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.
Follow any prompts or confirmations that appear during the installation process (Like installing NuGet …)
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.
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:
- Open PowerShell
- Type “Install-Module AzureAD”
- Press Y to accept the untrusted repository (PSGallery)
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