Share via

Programmatically (PowerShell) getting Windows versions.

Seak, Teng-Fong 161 Reputation points
2022-02-03T08:41:41.87+00:00

We have the GUI method to get Windows version and "version", ie Windows 8, 10 or 11 and for Windows 10, we have for example "version" 21H2.

So, what is the equivalence in program? Say in PowerShell, what is the command to get the value? I really mean the value, so that my script can run a if-then-else clause to do things differently according to the versions.

Thanks

Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments

4 answers

Sort by: Most helpful
  1. Castorix31 91,866 Reputation points
    2022-02-03T08:53:21.367+00:00

    To get 21H2 for example on Windows 10, you can do :

    (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DisplayVersion).DisplayVersion
    
    1 person found this answer helpful.

  2. Colin Chaplin 1 Reputation point
    2022-07-20T07:08:29.05+00:00

    Just like the add the voices saying 'wtf' (as if it'll make any difference)

    @Castorix31 your script worked for me once I got rid of space at the start of line 25 and 14.

    However, the results on Win11 are...interesting! Shortname is still windows 10, Longname is Windows 11 (Pro in my case)

    222621-image.png

    0 comments No comments

  3. Castorix31 91,866 Reputation points
    2022-02-04T10:14:59.95+00:00

    I made this PowerShell test script (.ps1), only tested on Windows 10, where I get :

    171362-powershell-windowsname.jpg

    Add-Type -TypeDefinition @"  
    using System;  
    using System.Diagnostics;  
    using System.Runtime.InteropServices;  
    public static class User32  
    {  
        [DllImport("User32.dll", CharSet=CharSet.Unicode)]  
            public static extern int MessageBox(  
                IntPtr hWnd,  
                String lpText,   
                String lpCaption,   
                int uType);   
    }  
    "@  
      
    Add-Type -TypeDefinition @"  
    using System;  
    using System.Diagnostics;  
    using System.Runtime.InteropServices;  
    public static class WinBrand  
    {           
            [DllImport("Winbrand.dll", CharSet = CharSet.Unicode)]        
            public static extern string BrandingFormatString(string sFormat);  
    }  
    "@  
      
    $sShortName = [WinBrand]::BrandingFormatString("%WINDOWS_SHORT%");  
    $sLongName = [WinBrand]::BrandingFormatString("%WINDOWS_LONG%");  
    $sMessage = "Short name = " + $sShortName  + "`n" + "Long name = " + $sLongName  
    [User32]::MessageBox(0, $sMessage, "Information", 0x00000040) | Out-Null  
    
    0 comments No comments

  4. EckiS 921 Reputation points
    2022-02-04T06:26:04.807+00:00

    you can try this;: but I have no Windows 11, so don't know what it returns
    Get-WMIObject win32_operatingsystem | select Caption, Version


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.