Azure & PowerShell Custom Code

Last Updated on August 9, 2024 by Arnav Sharma

I have been asked a few times about the easiest way to write a PowerShell script or how to get started writing a script and automating stuff in Azure. Or writing a complex script to automate a complex deployment.

(PS – This blog is just to help you expand you PS skills, and does not help in anyway if you’re a beginner)

The simple answer is Small Steps!! And Practice.

So, let us take an example, you have the following requirement and want to automate it in PowerShell:

Deploy a Virtual Network and three subnets in it.  Each subnet should have a network security group attached to it and the NSG should have allowed TCP inbound/outbound to rule attached. Subnet 1 should have a VM with public IP Address and all the traffic should be routed via a virtual machine. Here VM can be considered as a firewall that all traffic is always routed via the VM.

This is an easy task if you break this up into pieces (aka Small Steps).

Let’s break it apart and see what’s needed:

  • A resource Group
  • One Virtual Network
  • Three Subnets
  • Network Security Group
  • Inbound and Outbound Rules
  • Public IP
  • A virtual machine
  • Route Table.
  • Routing Rules

Finding commands using PowerShell ISE:

  1. Open PowerShell ISE and open the command ad-on, which open up on the right side:

2. In the name window, start with the resource you need to find. For example, creating a new network would be “New-Az..” followed by the resource name.

3. Select the command and click on Show Details. This should open up a window below and the properties marked with asterisks (*) are mandatory. Once all details are populated, copy and paste it to scripting areas as shown:

4. Do it for all the resources. And this is what it should look like:

Here are all the PS commands:

  • A resource Group: New-AzResourceGroup
  • One Virtual Network: New-AzVirtualNetworkSubnetConfig
  • Three Subnets: New-AzVirtualNetworkSubnetConfig
  • Network Security Group: New-AzNetworkSecurityGroup
  • Inbound and Outbound Rules: New-AzNetworkSecurityRuleConfig
  • Public IP: New-AzPublicIpAddress
  • A virtual Machine: New-AzVM
  • Route Table: New-AzRouteTable
  • Routing Rules: New-AzRouteConfig

5. Adding variables:

You can store all types of values in PowerShell variables. For example, store the results of commands, and store elements that are used in commands and expressions, such as names, paths, settings, and values.

A variable is a unit of memory in which values are stored. In PowerShell, variables are represented by text strings that begin with a dollar sign ($), such as $a, $process, or $my_var.

Variable names aren’t case-sensitive, and can include spaces and special characters. But, variable names that include special characters and spaces are difficult to use and should be avoided.

Remove all the static values and use variables instead.

6. Using Loops and Import data:

Loops can be used to execute the same function multiple times.

“For loop, For Each-Object loop, and the While, and Do-While”

For Loop:

For ($i=0; $i -le 10; $i++) {
    “10 * $i = ” + (10 * $i)
    }

For Each Object Loop:

The For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to $true.

A typical use of the For loop is to iterate an array of values and to operate on a subset of these values. In most cases, if you want to iterate all the values in an array, consider using a Foreach statement.

Importing files or reading data from files can be handy and the easiest way is using Import-CSV Command. Refer this below URL:

Import-Csv (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs

So, coming on to the example above – we need to create multiple subnets under the same vNET. This uses the same function/command.

The first step is to create a CSV file, similar to this:

And then import the file after which read one line at a time using a for loop.

The steps would now look like this:

  • Create a vNET (Step 1 below)
  • Import the CSV file (Step 2 below)
  • Run a loop (for each loop) (Step 3 below)
  • Create and attach subnets to the vNET. (Step 4 below)

Starting with the commands for each task, then breaking up into variables should give a good start writing a script. The next step is finding the task which are repetitive – once the tasks are figured out, the next step is either creating an Array (Google “PoweShell Arrays”) or import data using a file. Once you have minimized the amount of data/lines of code – you should be good to go.

Small steps everytime is the key !!

In the main example – you can use loops for multiple NSGs, create NSG rules (Can use CSV files for rules!!) or in case you want to create VM’s, you can have VM config (OS, SKU, Disk, etc) in a CSV file and then deploy VM’s


Q: What is Azure?

A: Azure is a cloud computing platform provided by Microsoft that allows users to build, deploy, and manage applications and services through a global network of data centers.

Q: What is PowerShell?

A: PowerShell is a command-line shell and scripting language developed by Microsoft for automating tasks in Windows environments and managing Microsoft products such as Azure.

Q: How can I use Azure with PowerShell?

A: You can use Azure PowerShell cmdlets and scripts to automate tasks and manage resources in Azure, such as creating virtual machines, managing storage accounts, and deploying applications.

Q: How can I create a virtual machine using Azure PowerShell?

A: To create a virtual machine using Azure PowerShell, you can use the New-AzVM cmdlet and specify the virtual machine name, image, size, and other parameters such as the resource group name and location.

Q: What are some examples of tasks I can automate using Azure PowerShell?

A: You can automate tasks such as creating and managing virtual machines, managing storage accounts, deploying and managing applications, and managing Azure resources such as virtual networks and security groups.

Q: How can I install Azure PowerShell?

A: You can install Azure PowerShell from the PowerShell Gallery using the Install-Module cmdlet. You can also download and install the Azure PowerShell module from the Azure portal.

Q: How do I connect to my Azure account using PowerShell?

A: You can connect to your Azure account using the Connect-AzAccount cmdlet and providing your Azure credentials. You can also connect to your Azure account using the Azure CLI or the Azure portal.

Q: What is Azure Resource Manager?

A: Azure Resource Manager is a management framework that allows you to deploy, manage, and monitor resources in Azure, such as virtual machines, storage accounts, and SQL databases. You can use Azure Resource Manager templates to define and deploy your resources in a repeatable and consistent manner.

Q: What are some best practices for writing PowerShell scripts in Azure?

A: Some best practices for writing PowerShell scripts in Azure include using descriptive variable names, commenting your code, using error handling and logging, and testing your scripts thoroughly before deploying them.

Q: How can I run PowerShell scripts in Azure?

A: You can run PowerShell scripts in Azure using Azure Automation, Azure Functions, or by running the script directly on an Azure virtual machine.

Q: What are the key features of the latest AZ PowerShell module update?

The latest AZ PowerShell module update, version 11.1.0, includes updated cmdlets like Set-AzDataFactoryV2IntegrationRuntime and New-AzSaaSNetworkVirtualAppliance. It also features the addition of cmdlets such as Remove-AzApplicationGatewayFirewallCustomRule and New-AzBastion. This update enhances the module’s capabilities for managing Microsoft Azure resources.

Q: How can users get started with deploying virtual machines (VMs) using the AZ 11.1.0 module?

To get started with deploying VMs using the AZ 11.1.0 module, users can utilize cmdlets like New-AzBastion and Set-AzBastion. These cmdlets help in setting up and managing Azure Bastion, a service that provides secure and seamless RDP and SSH access to virtual machines.

Q: What is the significance of the AZ PowerShell module in Microsoft Azure?

The AZ PowerShell module is crucial in Microsoft Azure as it provides a command-line interface for managing Azure resources. It supports a wide range of Azure services and allows for task automation, making it easy to learn and use for technical server management and configuration.

Q: What are the benefits of learning AZ PowerShell for cloud management?

Learning AZ PowerShell for cloud management offers numerous benefits, such as task automation, ease of configuration, and the ability to manage Azure resources directly from the command line. It’s an essential tool for anyone looking to efficiently manage and deploy resources in Microsoft Azure.

Q: How do the concepts of PowerShell and Bash differ in the context of Azure?

In the context of Azure, the concepts of PowerShell and Bash differ mainly in their syntax and operational environment. PowerShell, with its cmdlet-based approach, is deeply integrated with Windows environments, while Bash is more common in Linux and MacOS systems. Users can choose between PowerShell and Bash based on their operating system preferences and specific task requirements.

Q: What new features are included in the AZ PowerShell module version 11.1.0?

The AZ PowerShell module version 11.1.0 includes new features and updated cmdlets such as New-AzBastion, Set-AzBastion, Get-AzBastion, and updated support for encryptionAtHost. This release significantly enhances Azure cloud management capabilities.

Q: How does one install and configure the latest AZ PowerShell module for Azure?

To install and configure the latest AZ PowerShell module, version 11.1.0, for Azure, you can run the following cmdlets from a PowerShell interface: Install-Module -Name Az -RequiredVersion 11.1.0 and Import-Module Az. This process updates the module to the latest version, ensuring compatibility with Azure’s current features.

Q: Can the AZ PowerShell module be used on different operating systems like Linux or macOS?

Yes, the AZ PowerShell module supports various operating systems, including Linux and macOS. Users on these platforms can install the module through their respective package managers or use it in Docker containers, offering flexibility across different server environments.

Q: What resources are available for learning how to deploy applications using AZ PowerShell cmdlets?

For learning how to deploy applications using AZ PowerShell cmdlets, resources available include Microsoft Learn courses, GitHub repositories, and Azure’s official documentation. These provide comprehensive guidance on using cmdlets like Set-AzDataFactoryV2IntegrationRuntime and New-AzSaaSNetworkVirtualAppliance.

Q: What are the advantages of using PowerShell over traditional command-line tools in Azure?

The advantages of using PowerShell over traditional command-line tools in Azure include advanced task automation capabilities, a more intuitive scripting language that is easy to learn, and a rich set of cmdlets specifically designed for managing Azure resources. It allows for more complex and efficient management of cloud resources compared to basic command-line interfaces.

Q: How do users update to the latest PowerShell version for Azure management?

To update to the latest PowerShell version for Azure management, users can manually run the upgrade command in their PowerShell interface or browser-based Azure Cloud Shell. This ensures they have the most recent version with all the new features and support for current Azure services.

Q: What role does the concept of software automation play in Azure PowerShell?

The concept of software automation in Azure PowerShell plays a crucial role, as it streamlines cloud management tasks. With PowerShell’s scripting capabilities, users can automate repetitive tasks, configure resources, and manage deployments, making the process more efficient and error-free.

Q: Why is it important for users to be familiar with the installation and configuration of AZ PowerShell modules?

It is important for users to be familiar with the installation and configuration of AZ PowerShell modules because these steps are foundational for leveraging PowerShell’s full capabilities in Azure. Proper installation and configuration ensure that users can efficiently manage Azure resources, deploy applications, and automate tasks.

keywords: microsoft azure powershell net version of powershell output default powershell is a scripting language microsoft edge  browser az module updated cmdlet  flag added cmdlet icon

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.