,

How to install AZ Modules in Power shell. How to Connect Azure from PowerShell?

Posted by

Determine if the version of PowerShell you’re running meets the minimum requirements before attempting to install the Az PowerShell module.

$PSVersionTable.PSVersion

The .NET Framework 4.7.2 or higher is also required by the Az PowerShell module on Windows. 

Verify the .NET Framework 4.7.2 or higher is installed.

(Get-ItemProperty -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full' -ErrorAction SilentlyContinue).Release -ge 461808

Installation only for Admin User

The installation itself is simple from the PowerShell Gallery.

  • Open PowerShell as Administrator:
  • Launch PowerShell as an administrator. To do this, right-click on the PowerShell icon and select “Run as administrator.”
  • Check if the Az Module is Already Installed:
  • You can check if the Az module is already installed by running the following command:
  • If it returns nothing, it means the Az module is not installed
Get-InstalledModule -Name Az 

This will install the Az module for all users and requires admin rights. 

Install the Az Module:

Confirm Installation:

Get-InstalledModule -Name Az,

 

Installation only for Current User

Recommends only installing it for the current user by specifying the Scope parameter with CurrentUser as its value which does not require admin rights.

You can check if the Az module is already installed for the current user by running the following command:

Get-InstalledModule -Name Az -Scope CurrentUser -ErrorAction SilentlyContinue
  • If it returns nothing, it means the Az module is not installed for the current user.

Install the Az Module:

  • To install the Az module for the current user, use the Install-Module cmdlet with the -Scope CurrentUser parameter:

Install-Module -Name Az -Scope CurrentUser -AllowClobber -Force
  • -Name Az: Specifies the name of the module to install (which is “Az” for the Azure module).
  • -Scope CurrentUser: Specifies that the module should be installed for the current user only.
  • -AllowClobber: Allows overwriting existing modules (if any).
  • -Force: Forces the installation without prompting for confirmation.

Confirm Installation:

  • You may be prompted to trust the repository. Type “Y” to confirm and proceed with the installation.

Verify Installation:

  • After the installation is complete, you can verify that the Az module is installed by running:

Get-InstalledModule -Name Az -Scope CurrentUser


This command should return information about the installed Az module.


Import the Az module

Import-Module -Name Az

If getting below error than follow given steps:

> Import-Module Az.Accounts
Import-Module : File C:\Users\mgufranja1\Documents\WindowsPowerShell\Modules\Az.Accounts\2.13.1\Az.Accounts.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module Az.Accounts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Import-Module], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

Check about_Execution_Policies

To get all of the execution policies that affect the current session and display them in precedence order:

Get-ExecutionPolicy -List

Output:

Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine AllSigned

For example, the following command gets the execution policy for the CurrentUser scope:

Get-ExecutionPolicy -Scope CurrentUser

Change the execution policy

change the execution policy if current user is undefined

To change your execution policy:

Set-ExecutionPolicy -ExecutionPolicy

For example:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

To set the execution policy in a particular scope:

Set-ExecutionPolicy -ExecutionPolicy <PolicyName> -Scope <scope>

For example:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

A command to change an execution policy can succeed but still not change the effective execution policy.

IF running below command you will get result:

Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser RemoteSigned
LocalMachine AllSigned

Now run

Import-Module -Name Az

How to connect Azure portal from powershell

Sign in to Azure:

  • Once the module is installed, you can sign in to your Azure account using the Connect-AzAccount cmdlet:
Connect-AzAccount
  • A dialog box will appear, prompting you to enter your Azure credentials. Sign in with your Azure username and password, or follow the instructions for device login, depending on your authentication method.
  • After successful authentication, your Azure subscription(s) will be displayed in the PowerShell window

Select an Azure Subscription (Optional):

  • If you have multiple Azure subscriptions associated with your account, you can select a specific subscription to work with using the Set-AzContext cmdlet:
Set-AzContext -SubscriptionName "YourSubscriptionName"


Replace “YourSubscriptionName” with the name of the Azure subscription you want to use. This step is optional if you have only one subscription.

Verify the Connection:

  • You can verify that you are connected to Azure and see the details of your Azure account by running the following command:

Get-AzContext


This command will display information about the connected Azure account and subscription.

Some helpful PowerShell commands and cmdlets to perform various operation in Azure

Azure Account and Subscription Management:

  • Connect-AzAccount: Sign in to your Azure account.
  • Get-AzSubscription: List all available Azure subscriptions.
  • Set-AzContext -SubscriptionName <SubscriptionName>: Select a specific Azure subscription.

Resource Group Management:

  • New-AzResourceGroup: Create a new Azure resource group.
  • Get-AzResourceGroup: List resource groups in your subscription.
  • Remove-AzResourceGroup -Name <ResourceGroupName> -Force: Delete a resource group and its resources.

Azure Resource Management:

  • New-AzResource: Create a new Azure resource.
  • Get-AzResource: Retrieve Azure resource details.
  • Remove-AzResource: Delete an Azure resource.

Virtual Machine (VM) Management:

  • New-AzVm: Create a new virtual machine.
  • Get-AzVM: List virtual machines in your subscription.
  • Start-AzVM and Stop-AzVM: Start or stop a virtual machine.

Storage Account Management:

  • New-AzStorageAccount: Create an Azure storage account.
  • Get-AzStorageAccount: List Azure storage accounts.
  • Remove-AzStorageAccount: Delete an Azure storage account.

Azure Web App Management:

  • New-AzWebApp: Create a new Azure web app.
  • Start-AzWebApp and Stop-AzWebApp: Start or stop an Azure web app.

Azure SQL Database Management:

  • New-AzSqlDatabase: Create a new Azure SQL database.
  • Get-AzSqlDatabase: List Azure SQL databases.
  • Start-AzSqlDatabase and Stop-AzSqlDatabase: Start or stop an Azure SQL database.

Network Management:

  • New-AzVirtualNetwork: Create a new virtual network.
  • New-AzNetworkSecurityGroup: Create a new network security group.
  • Get-AzNetworkSecurityGroup: List network security groups.

Azure Functions Management:

  • New-AzFunctionApp: Create an Azure Functions app.
  • Get-AzFunctionApp: List Azure Functions apps.
  • Remove-AzFunctionApp: Delete an Azure Functions app.

Azure Key Vault Management:

  • New-AzKeyVault: Create an Azure Key Vault.
  • Get-AzKeyVault: List Azure Key Vaults.
  • Set-AzKeyVaultSecret: Set a secret in an Azure Key Vault.

Azure Policy Management:

  • Get-AzPolicyDefinition: List Azure Policy definitions.
  • Set-AzPolicyAssignment: Assign a policy to a resource.
  • Get-AzPolicyComplianceStatus: Check policy compliance status.

Resource Lock Management:

  • New-AzResourceLock: Create a resource lock.
  • Remove-AzResourceLock: Remove a resource lock.
  • Get-AzResourceLock: List resource locks.

Azure Active Directory (AD) and Identity Management:

  • New-AzADUser: Create an Azure AD user.
  • Get-AzADGroup: List Azure AD groups.
  • Set-AzADApplication: Set Azure AD application properties.

Monitoring and Logging:

  • Get-AzMetricDefinition: List metric definitions for a resource.
  • Get-AzLog: Retrieve Azure activity logs.

These commands cover various aspects of Azure management, including virtual machines, storage, web apps, databases, network, functions, security, and identity. Be sure to customize these commands to your specific needs and refer to Azure documentation and resources for detailed usage and examples.

guest
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dharmendra kumar
Dharmendra kumar
10 months ago

I’m new to Azure, and your blog was a perfect introduction. Your writing style is approachable, and you demystified a topic that seemed complex. I appreciate the effort you put into creating this guide. Keep up the excellent work!

rakesh
rakesh
10 months ago

thanks for sharing the blog, the presentation ,command and structure of blog is very informative manner that solve the my bug yesterday the steps are very useful and very clear representation with points

Amit Kumar
Amit Kumar
10 months ago

Thanks your explaination and your efforts its very helpfull for us !!

Maruti Kumar
Maruti Kumar
10 months ago

This article is a goldmine of information! I appreciate the depth and clarity of your explanations. Thanks for sharing.

Roshan Jha
Roshan Jha
10 months ago

Very helpful blogs for us…. please continue or share your knowledge from this type of blogs… Thankyou!!!

Anil Kumar
Anil Kumar
10 months ago

In the world of Azure, your guide is a beacon of clarity. An invaluable resource for those navigating the Azure-PowerShell landscape.

Vijay
Vijay
10 months ago

“Thank you for sharing this informative post! I’m definitely going to be coming back to your blog for more.”

smith
smith
10 months ago

The post provides a simple step-by-step guide on how to connect to Azure from PowerShell, using the Connect-AzAccount cmdlet.

Avinash kumar
Avinash kumar
10 months ago

Very informative post! I appreciate the clear instructions on installing AZ modules and connecting to Azure through PowerShell.

Abhishek singh
Abhishek singh
10 months ago

The Az PowerShell module is the recommended way to manage Azure resources from PowerShell. It provides a more consistent and updated interface than the AzureRM PowerShell module.

Ashwani
Ashwani
10 months ago

thanks for sharing.. i have follow all the steps given by you and it really works for me, I have gone through many blogs but no one works.. thanks a lot for this i really apricate you work

11
0
Would love your thoughts, please comment.x
()
x