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

PowerShellMicrosoft TechnologiesSoftware & Coding

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

Example

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.

Example

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

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.

Example

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

Output

Use the below command,

Example

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.

Output

PS C:\> $osver = $vm.StorageProfile.ImageReference.Offer + " $($vm.StorageProfile.ImageReference.Sku)"
PS C:\> $osver
WindowsServer 2016-Datacenter
raja
Published on 12-Apr-2021 11:19:16
Advertisements