Creating Azure Virtual Machine using PowerShell
Tags: powershell, azure
Published: 2022-07-21 05:03:34

**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](https://www.powershellgallery.com/):
- [Az.Accounts](https://www.powershellgallery.com/packages/Az.Accounts/) - Used to connect to Azure Account through PowerShell.
- [Az.Resources](https://www.powershellgallery.com/packages/Az.Resources/) - Used to create/get the Resource Group for out VM.
- [Az.Compute](https://www.powershellgallery.com/packages/Az.Compute/) - Used to create/connect the Virtual Machine.

Use the following syntax to install these modules:
```
Install-Module -Name ModuleName
```

Let's start by creating a PowerShell file first. Open [Visual Studio Code](https://code.visualstudio.com/) or PowerShell ISE. You can install the [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell) 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
```
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
```
This command will show you some similar results to this, confirming your account and subscription.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcx9yh7a9sagbrmeby51.png)

If you want, you can create a new Resource Group for your VM using the command:
```
New-AzResourceGroup -Name "VM_RG" -Location "CentralIndia"
```
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)
```
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:
{% gist https://gist.github.com/aniket-sinha8/dc16b2a3422ac3d5dfc668d3bd56dea9 %}


Now you can connect/start your VM using RDP: [Connecting to Windows Virtual Machine created on Azure using RDP
](https://dev.to/aniketkumarsinha/connecting-to-windows-virtual-machine-created-on-azure-using-rdp-3ed3)

Docs on the commands used:
- [Connect-AzAccount](https://docs.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-8.1.0)
- [Get-AzContext](https://docs.microsoft.com/en-us/powershell/module/az.accounts/get-azcontext?view=azps-8.1.0)
- [New-AzResourceGroup](https://docs.microsoft.com/en-us/powershell/module/az.resources/new-azresourcegroup?view=azps-8.1.0)
- [New-AzVM](https://docs.microsoft.com/en-us/powershell/module/az.compute/new-azvm?view=azps-8.1.0)

Hurray🥳
Enjoy using your VM now!

{% user aniketkumarsinha %}