Deploy a Virtual Machine using Azure PowerShell
Tags: azure, 100daysofcloud, powershell, cloud
Published: 2024-01-21 21:32:32

Today is Day 6 of my 100 Days of Cloud challenge, and I'm going to repeat what I did yesterday, but with a twist.

On Day 5, I put a Virtual Machine into Azure using the Portal and explained each step. Today, I'm going to use PowerShell to do the same thing. This means that instead of clicking around a lot (and making you scroll endlessly, dear reader), I can now accomplish the task with just one command.


![Azure Powershell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e113tv41p086uda0uq6.png)

I'm using portal.azure.com powershell here, as if any user is working on MAC, this would help everyone feels same. Doing this helps us to use some less commands, like you don't have to write a command for Azure Connection. So, let's go with the show on the road. 

```
get-help New-AzVM
```


![Commands here](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqbb9rtzw4ururvicbbm.png)

Certainly! Breaking it down:

Wow, that's a ton of information, and when it all shows up on the screen like that, it can be overwhelming. However, let's focus on some important details.

Firstly, check out the "SYNTAX" section. Here, you'll see a list of parameters that you can use with "New-AzVM." These parameters should look familiar because they're the same things I used when creating the VM in the Portal.


You've already created a Resource Group using PowerShell on Day 3, and you know its name. Also, you've identified that your location is "eastus" as indicated in the output of "Get-AzVM."

Given this, the PowerShell command for this part would be:

![Powershell location cmd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whnd0qpsyapy52xxiowa.png)

Secondly, if we check the "REMARKS" section, there are additional commands we can run, and one of them provides examples. Let's run that command and see what it gives us.


![Example 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5akidftalh1twwrr3sq.png)

**Example 2** : 

![Example 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmxn64ow2a0wl02ydbbq.png)

Now, there are typically two ways by which you can make a VM here, but first you've set the credentials first, there is one method by using -Verbose as a parameter that allows you to create a VM with default configurations.

You must have noticed about several examples, and the first one seems surprisingly straightforward, just asking for a Name parameter for the VM and my credentials. It can't be that easy, can it? Let's give it a shot— I'll run this with a "-Verbose" switch to get some output.


![Credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4ixr2cocoi59ihrmfai.png)


But as soon you run it, you'll see this: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szphrqvu5i1kphaw9hw8.png)
 

Run these commands to set this up : 

```
$securePassword = ConvertTo-SecureString 'Changethepasswordhere' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('Changetheuserhere', $securePassword)
```


![Setup Credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzd1wqy3cp8dszw7w11k.png)


Here we go with the results: 

![User Password is set now](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oy5wv57y5lqhlqmlyvue.png)

Let's deploy the VM now: 

Either you copy the command or you know your VM type you want to deploy alike those examples we saw earlier you can always edit those. Or for learning purpose, you can use the default VM Machine, use the -Verbose parameter. 

```
New-AzVM -Name MYPSTestVM -Credential (Get-Credential) -Verbose
```

![Verbose utilization](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lceqxbjvdxgjbhty5txn.png)


This is how you do it, now wait for this to finish & all set. 
![VM all set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blsyz3a2fm0aepfuv5g2.png)

```
Get-AzVM
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sowhx1mp2wkgrws1ezxq.png)


And check the Portal, there’s the new Resource Group:


![Resource Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpgdmqfoogntkuwr3zto.png)

And if I click into that, there’s all my resources:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6bt9egxsp9xgz0l7lgw.png)


This is why understanding the parameters is crucial; it ensures that the Virtual Machine we create is configured the way we want it. Checking the machine in the Azure Pricing Calculator, including a Windows License and SSD. While Microsoft provides defaults, for testing purposes, I initially considered deleting it.

Moving forward, I want to create a new VM in the correct Resource Group with specific options. Looking at the "-Verbose" output from creating the first VM guides me on the parameters and options I need. I want to specify the following:

- Resource Group: MyExamplePowerShellRG2
- Location: eastus
- Name: MyPowerShellVM (VM name in the portal, not the local computer name)
- AddressPrefix: "10.30.0.0/16" (for the Virtual network)
- SubnetName: PSVMSubnet
- SubnetAddressPrefix: "10.30.30.0/24"
- PublicIPAddressName: PSPublicIP
- DomainNameLabel: psvm001md (local)
- SecurityGroupName: PSVMNSG
- OpenPorts: 3389 (for RDP Connectivity, or SSH for Linux)
- ImageName: "Win2016DataCenter"
- Size: "Standard_B2s" (VM Size; a full list can be found [here](https://docs.microsoft.com/en-us/azure/virtual-machines/sizes))
- OSDiskDeleteOption: Delete (specifies whether the OS Disk is deleted or detached and retained when the VM is deleted; options are Delete, Detach)

Referring to "get-help" for the command or the official Microsoft Docs article for "New-AzVM" reveals that these are just a few options available, but they are likely the most common ones. With these options, my PowerShell command should look like this:

```powershell
New-AzVM -ResourceGroupName MyExamplePowerShellRG2 -Location eastus -Name MyPowerShellVM -AddressPrefix "10.30.0.0/16" -SubnetName PSVMSubnet -SubnetAddressPrefix "10.30.30.0/24" -PublicIPAddressName PSPublicIP -DomainNameLabel psvm001md -SecurityGroupName PSVMNSG -OpenPorts 3389 -ImageName "Win2016DataCenter" -Size "Standard_B2s" -OSDiskDeleteOption Delete
```

Now, let’s delete the VM. I’ll run

```
get-help Remove-AzVM

```

**Removing Resource Group: **


```
Remove-AzVM -ResourceGroupName MyExamplePowerShellRG2 -Name MyPowerShellVM

```



And all set, now you're Scripting dude, writing your first Powershell script today for making these Virtual Machines. 

See you tomorrow with Azure CLI.