DEV Community

Aniket Kumar Sinha for This is Learning

Posted on

Creating Azure Virtual Machine using PowerShell

What is Virtual Machine?
Virtual machine is a virtual environment over your a system. Assume you have a Linux OS on your system, but you want to use Windows for some specific functionality, but you don't want to install it over your system. What you can do is, just create a VM (Virtual Machine) with Windows OS and use in whatever way you want virtually over your system.
So basically, a virtual machine (VM) is a virtual environment that functions as a virtual computer system with its own CPU, memory, network interface, and storage, created on a physical hardware system.

What is PowerShell?
PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework.

So, now let's create a VM using PowerShell.

Before starting, you need to install some modules from the PowerShell Gallery:

  • Az.Accounts - Used to connect to Azure Account through PowerShell.
  • Az.Resources - Used to create/get the Resource Group for out VM.
  • Az.Compute - Used to create/connect the Virtual Machine.

Use the following syntax to install these modules:

Install-Module -Name ModuleName
Enter fullscreen mode Exit fullscreen mode

Let's start by creating a PowerShell file first. Open Visual Studio Code or PowerShell ISE. You can install the PowerShell extension in the VS Code. Create a PowerShell file with extension .ps1. For example VMusingPS.ps1.

Now we need to connect to our Azure account using PowerShell script. Run the following command in VS Code.

Connect-AzAccount
Enter fullscreen mode Exit fullscreen mode

Select the command and then tap on the key F8 to run this.
This command will open up an interface in your browser to sign-in to your Azure account.

To confirm your connection, run the command:

Get-AzContext
Enter fullscreen mode Exit fullscreen mode

This command will show you some similar results to this, confirming your account and subscription.
Image description

If you want, you can create a new Resource Group for your VM using the command:

New-AzResourceGroup -Name "VM_RG" -Location "CentralIndia"
Enter fullscreen mode Exit fullscreen mode

Or use any existing Resource Group.

I am using the existing Resource Group named as "VM_RG".
Run the following command to create a Virtual Machine machine using PowerShell:

New-AzVM -Name "VMusingPS" -ResourceGroupName "VM_RG" -Location "CentralIndia" -Credential (Get-Credential)
Enter fullscreen mode Exit fullscreen mode

Since the VM needs a credential for it's access, so after running this command, it will ask for the username and password for your VM. Set your credentials and press Enter, and done.

You can confirm by checking your mentioned Resource Group, it will show your Virtual Machine there.

You have successfully created an Azure Virtual Machine using PowerShell script.

Here's the full code:

Now you can connect/start your VM using RDP: Connecting to Windows Virtual Machine created on Azure using RDP

Docs on the commands used:

HurrayπŸ₯³
Enjoy using your VM now!

Top comments (0)