Azure PowerShell is a powerful tool that can be used to create scripts to automate Azure management tasks. In this article, you can find the most commonly used Azure PowerShell and Azure Active Directory (Azure AD) PowerShell commands, along with the installation steps.
In which operating system can we use Azure PowerShell?
Azure PowerShell modules can be installed on computers or Virtual machines that runs Windows, Linux or Mac OS. It can be used to create scripts to automate azure management tasks.
What needs to be installed on your machine to let you execute Azure PowerShell cmdlets locally?
You need to Install Azure PowerShell and Azure AD PowerShell Modules on your machine to let you execute azure PowerShell cmdlets locally.
Install Azure PowerShell Modules:
Installing azure PowerShell from the PowerShell gallery is super easy. Just follow the steps to install Azure "Az" PowerShell modules.
Step 1: Open PowerShell as Administrator and run the following command to set the PowerShell execution policy to remote signed.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Step 2: Run the following command to install the latest version of the Az PowerShell module to PSGallery Repository at force.
Install-Module -Name Az -Repository PSGallery -Force
For this NuGet provider is required if it ask for NuGet provider type 'Y' to continue.
How to check Azure PowerShell Version?
If you wish to check your Azure PowerShell version then use the command:
Get-InstalledModule -Name Az -AllVersions | select Name,Version
How to check modules included in Azure PowerShell?
Get-InstalledModule -Name Az -AllVersions | select Name,Version
How to connect to Azure with PowerShell?
Once you are done with installing the Azure Az modules you can login to Azure PowerShell using the following cmdlet:
Connect-AzAccount
How to Logout from Azure PowerShell?
Disconnect-AzAccount cmdlet will will disconnect the current session from Azure and remove all credentials and contexts associated with that account.
Disconnect-AzAccount
You can also specify a specific account to disconnect from by using the -Username parameter. For example, the following command will disconnect the account with the username myusername.
Disconnect-AzAccount -Username myusername
How to set a subscription in Azure PowerShell?
The following command is used to switch, set, or change a subscription in Azure PowerShell:
Set-AzContext -Subscription '<add subscription ID or subscription name>'
How to get subscription id Azure PowerShell?
To get the subscription ID in Azure PowerShell, you can use the (Get-AzContext).Id cmdlet. This will return the ID of the subscription that you are currently logged into.
(Get-AzContext).Id
How to list subscriptions Azure PowerShell?
The Get-AzSubscription cmdlet will list all of the subscriptions that you have access to.
Get-AzSubscription
Install Azure AD PowerShell Module:
Use this cmdlet to install Azure AD PowerShell module:
Install-Module -Name AzureAD -Repository PSGallery -Force
The -Repository PSGallery parameter tells PowerShell to install the module from the PowerShell Gallery. The -Force parameter tells PowerShell to overwrite any existing version of the module.
How to connect to Azure AD PowerShell?
The Connect-AzureAD command connects to your Azure AD tenant. You will need to provide your tenant ID and credentials when you run this command.
Connect-AzureAD
How to disconnect from Azure AD PowerShell?
The Disconnect-AzureAD command disconnects from your Azure AD tenant.
Disconnect-AzureAD
PowerShell Quick Start Guide:
How to create a folder in PowerShell?
Method 1: Using New-Item cmdlet
This is the most common way to create a folder in PowerShell. Here is the syntax for New-Item cmdlet:
New-Item -Path <path> -ItemType Directory
Example: o create a folder named "MyTestFolder-01" in the current directory, you would use the following command:
New-Item -Path . -ItemType Directory -Name MyTestFolder-01
Method 2: Using mkdir command
This is a legacy command that can still be used to create folders in PowerShell. Here is the syntax for mkdir command:
mkdir <path>
Example: create a folder named "MyTestFolder-02" in the current directory, you would use the following command:
mkdir MyTestFolder-02
Method 3: Using PowerShell Script
You can also create a folder in PowerShell using a script. Here is an example PowerShell script that creates a folder named "MyTestFolder-03" in the current directory.
Create a PowerShell script and save the script as CreateFolder.ps1
code CreateFolder.ps1
Copy and save the below script to CreateFolder.ps1 file
# Define the script
$path = "."
$name = "MyTestFolder-03"
# Create the folder
New-Item -Path $path -ItemType Directory -Name $name
To run the script, open a PowerShell console and type the following command:
.\CreateFolder.ps1
How to create a file in PowerShell?
- Method 1: New-Item <filename>.<extension>
- Method 2: touch <filename>.<extension>
- Method 3: Code <filename>.<extension>
How to run Azure PowerShell script?
.\<filename>.<extension>
Related Articles:
👉 How to Find Azure VM Username and Password
👉 Install Azure CLI on Windows (Simple & Best Method)