To retrieve the Azure VM OS using Azure CLI, we can use the “az vm” command but before that, need to make sure that you are connected to the Azure cloud and the subscription.
PS C:\> az vm show -n VMName -g VMRG --query "[storageProfile.imageReference.offer]" -otsv
OR
PS C:\> az vm show -n VMName -g VMRG --query storageProfile.imageReference.offer - otsv
WindowsServer
To get the OS SKU or the operating system version, you can use,
PS C:\> az vm show -n VMName -g VMRG --query "[storageProfile.imageReference.sku]" -otsv
2016-Datacenter
You can also use the below command to get the OS of the VM without providing the resource group name.
PS C:\> az vm list --query "[?name==VMName].{os:storageProfile.imageReference.offer}" -otsv
To get the OS version or SKU,
PS C:\> az vm list --query "[?name==VMName].{os:storageProfile.imageReference.sku}" -otsv
To combine all the outputs in the table format,
az vm list --query "[].{vmName:name, ResourceGroup:resourceGroup, os:storageProfile.imageReference.offer, version:storageProfile.imageReference.sku}" - otable