Creating virtual machines in Azure using PowerShell

create azure vm via powershell

To create a virtual machine in Azure using PowerShell, you will need to have the Azure PowerShell module installed and set up on your local machine. You can find instructions for installing and configuring the Azure PowerShell module at this link:

https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-2.8.0

  • Once you have the Azure PowerShell module installed, open a PowerShell window and sign in to your Azure account using the Connect-AzAccount cmdlet. You will be prompted to enter your Azure login credentials.
  • Next, create a new resource group to host your virtual machine. You can use the New-AzResourceGroup cmdlet to create a new resource group with a specified name and location:
$resourceGroupName = "myResourceGroup"
$location = "East US"
New-AzResourceGroup `
-Name $resourceGroupName `
-Location $location
  • Now you are ready to create the virtual machine. Use the New-AzVM cmdlet to create a new virtual machine with a specified resource group, VM name, location, size, and image:
$vmName = "myVM"
$vmSize = "Standard_D2s_v3"
$imageName = "UbuntuLTS"
New-AzVM `
-ResourceGroupName $resourceGroupName `
-Name $vmName `
-Location $location `
-Size $vmSize `
-ImageName $imageName

This will create a new virtual machine with the specified resource group, name, location, size, and image.

  • You can also specify additional parameters, such as the administrator username and password, the network security group, and the public IP address, using the -AdminUsername, -AdminPassword, -NetworkSecurityGroup, and -PublicIpAddress parameters, respectively. For example:
$adminUsername = "adminuser"
$adminPassword = "MyStrongPassword!"
New-AzVM `
-ResourceGroupName $resourceGroupName `
-Name $vmName `
-Location $location `
-Size $vmSize `
-ImageName $imageName `
-AdminUsername $adminUsername `
-AdminPassword $adminPassword `
-NetworkSecurityGroup "myNSG" `
-PublicIpAddress "myPublicIP"

This will create a new virtual machine with the specified resource group, name, location, size, image, administrator username and password, network security group, and public IP address.

  • Once the virtual machine has been created, you can start it using the Start-AzVM cmdlet:
Start-AzVM `
-ResourceGroupName $resourceGroupName `
-Name $vmName

For more information about the New-AzVM cmdlet and the available parameters, you can refer to the Azure PowerShell documentation:

https://docs.microsoft.com/en-us/powershell/module/az.compute/new-azvm?view=azps-2.8.0

Was this helpful?

1 / 0

Leave a Reply 0

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