How to retrieve the Operating system of the Azure VM using PowerShell?

To retrieve the OS details of the Azure VM, we need to use the Get-AzVM command.


Get-AzVM -VMName TestMachine2k16

When you run the above command, it retrieves the VM TestMachine2k16 information and there is an OSType property which shows that if the VM’s OS is Linux or Windows, or any other type.

But when you select the OSType, you won’t get anything. See below.


PS C:\> Get-AzVM -VMName TestMachine2k16 | Select OStype


Because this property is a part of another property and hence can’t be accessed directly. When you expose the full properties of the VM, you will get the StorageProfile, which contains the OS information.


PS C:\> $vm = Get-AzVM -VMName TestMachine2k16
PS C:\> $vm | fl *


Use the below command,


PS C:\> $vm.StorageProfile.ImageReference

Publisher    : MicrosoftWindowsServer
Offer        : WindowsServer
Sku          : 2016-Datacenter
Version      : latest
ExactVersion : 14393.4225.2102030345
Id           :

So here we got both the Server Edition and the version.


PS C:\> $osver = $vm.StorageProfile.ImageReference.Offer + " $($vm.StorageProfile.ImageReference.Sku)"
PS C:\> $osver
WindowsServer 2016-Datacenter