Step 1: Install and Configure Azure PowerShell
If you haven’t already, you need to install Azure PowerShell module and connect to your Azure subscription. You can do this using the following commands:
# Install Azure PowerShell module (if not already installed) Install-Module -Name Az -AllowClobber -Force # Connect to your Azure account (login) Connect-AzAccount
Follow the prompts to log in to your Azure account.
Step 2: Create a Resource Group
Azure resources are organized into resource groups. You can create a resource group using the following command:
$resourceGroupName = "YourResourceGroupName" $location = "East US" # Replace with your desired Azure region New-AzResourceGroup -Name $resourceGroupName -Location $location
Replace “YourResourceGroupName” with a name for your resource group and choose a suitable Azure region.
Step 3: Create a Virtual Network and Subnet
You’ll need a virtual network and subnet for your VM. Here’s how to create them:
$vnetName = "YourVNetName" $subnetName = "YourSubnetName" $addressPrefix = "10.0.0.0/16" $subnetPrefix = "10.0.0.0/24" New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName -Location $location -AddressPrefix $addressPrefix New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetPrefix -VirtualNetwork (Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName) | Set-AzVirtualNetwork
Replace “YourVNetName” and “YourSubnetName” with appropriate names and address prefixes.
Step 4: Create a Public IP Address
Your VM will need a public IP address to be accessible over the internet:
$publicIpName = "YourPublicIpName" New-AzPublicIpAddress -Name $publicIpName -ResourceGroupName $resourceGroupName -Location $location -AllocationMethod Dynamic
Step 5: Define VM Configuration
Now, let’s create a configuration for your VM:
$vmName = "YourVMName" $vmSize = "Standard_B2s" # Choose an appropriate VM size $adminUsername = "YourAdminUsername" $adminPassword = "YourAdminPassword" $vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize $vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential (Get-Credential -UserName $adminUsername -Password $adminPassword) $vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "latest" $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id (Get-AzNetworkInterface -ResourceGroupName $resourceGroupName | Where-Object { $_.VirtualMachine.id -eq $null }).Id $vmConfig = Set-AzVMBootDiagnostics -VM $vmConfig -Disable
Replace “YourVMName,” “YourAdminUsername,” and “YourAdminPassword” with appropriate values.
Step 6: Create the Virtual Machine
Now, you can create the VM:
New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig -Verbose
This command will create the VM using the configuration you specified.
Step 7: Access Your VM
Once the VM is created, you can access it using Remote Desktop Protocol (RDP) or SSH, depending on the operating system you chose (Windows or Linux).
That’s it! You’ve created a virtual machine in Azure using PowerShell. Make sure to replace the placeholders with your actual values, and customize the VM configuration to your specific requirements.