http://www.perlmonks.org?node_id=1005106

bimleshsharma has asked for the wisdom of the Perl Monks concerning the following question:

I have to find out version number OS(windows) that is installed on box using perl. I tried below but i could not get version number of even OS. Help is much appreciated.

use English qw' -no_match_vars '; print "$OSNAME\n";

Replies are listed 'Best First'.
Re: Get version of installed OS
by marto (Cardinal) on Nov 22, 2012 at 11:25 UTC

    Perhaps this does what you want, it uses Config which is core. Tested on Windows, Solaris and netbsd:

    #!/usr/bin/perl use strict; use warnings; use Config; print "$Config{osname}\n"; print "$Config{archname}\n"; print "$Config{osvers}\n";

    Output:

    MSWin32 MSWin32-x86-multi-thread 5.1

      Thanks.... One another way to get specially for windows is

      my $osname = Win32::GetOSName(); my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion(); print "osname: $osname\n"; print "osvername: $osvername\n"; print "major: $major\n"; print "minor: $minor\n"; print "id: $id\n";

        True, though you didn't specify which OS you were using when you posted the question ;)

        Update: I see you've just changed your question to specify OS. It'd make more sense if you'd posted that as an update to your post, like I've done here.

Re: Get version of installed OS
by GrandFather (Saint) on Nov 22, 2012 at 11:48 UTC

    You can use Win32 to get the information you want then a little playing around to get a OS name:

    #!/usr/bin/perl use warnings; use strict; use Win32; my %versions = ( 0 => {'' => {'' => "Win32s"}}, 1 => { 4 => { 0 => "Windows 95", 10 => "Windows 98", 90 => "Windows Me" } }, 2 => { 3 => {51 => "Windows NT 3.51"}, 4 => {0 => "Windows NT 4"}, 5 => { 0 => "Windows 2000", 1 => "Windows XP", 2 => "Windows Server 2003" }, 6 => { 0 => "Windows Vista or Server 2008", 1 => "Windows 7" } }, ); my ($name, $major, $minor, $build, $id) = Win32::GetOSVersion(); print $versions{$id}{$major}{$minor};

    Actually a little more work than that is required to do a proper job, but it is well documented in Win32::GetOSVersion() docs linked above.

    True laziness is hard work
Re: Get version of installed OS
by borodache (Novice) on Feb 26, 2018 at 14:57 UTC

    Hi All,
    I am trying to see if the Windows version I am using is newer than Windows 7 or Windows Server 2008 R2 (minimum requirements). I googled a bit and run into your post. Here is my question, I am running on Windows 10 machine and these lines:

    print ("(\$ENV{\"OS\"}: " . ($ENV{"OS"}) . "\n"); use Win32; print "GetOSName: " . Win32::GetOSName() . "\n"; use Config; print "$Config{osname}\n"; print "$Config{archname}\n"; print "$Config{osvers}\n"; my ($name, $major, $minor, $build, $id) = Win32::GetOSVersion(); my %versions = ( 0 => {'' => {'' => "Win32s"}}, 1 => { 4 => { 0 => "Windows 95", 10 => "Windows 98", 90 => "Windows Me" } }, 2 => { 3 => {51 => "Windows NT 3.51"}, 4 => {0 => "Windows NT 4"}, 5 => { 0 => "Windows 2000", 1 => "Windows XP", 2 => "Windows Server 2003" }, 6 => { 0 => "Windows Vista or Server 2008", 1 => "Windows 7" } }, ); print "version: " . $id . ", " . $major . "," . $minor . "\n";
    yields me:
    ($ENV{"OS"}: Windows_NT GetOSName: Win2003 MSWin32 MSWin32-x86-multi-thread 4.0 version: 2, 6,2
    My questions are:
    1) How can I see it is windows 10? Why do I receive "Windows_NT" and "Windows2003" in my first two lines?
    2) @marto why do I get the version to be 4.0?
    3) @GrandFather, I looked into the link you posted http://search.cpan.org/~jdb/Win32-0.52/Win32.pm, but all I see there is a table of:
    OS ID MAJOR MINOR Win32s 0 - - Windows 95 1 4 0 Windows 98 1 4 10 Windows Me 1 4 90 Windows NT 3.51 2 3 51 Windows NT 4 2 4 0 Windows 2000 2 5 0 Windows XP 2 5 1 Windows Server 2003 2 5 2 Windows Server 2003 R2 2 5 2 Windows Home Server 2 5 2 Windows Vista 2 6 0 Windows Server 2008 2 6 0 Windows 7 2 6 1 Windows Server 2008 R2 2 6 1 Windows 8 2 6 2 Windows Server 2012 2 6 2
    Where is Windows 10, 2016 and other which are newer (regular/server) from the requirement I wrote?

      Might System::Info help?

      C:\> cpan System::Info C:\> perl -MSystem::Info -wE"say System::Info->new->os" MSWin32 - Win7 Professional (64-bit) SP1

      Enjoy, Have FUN! H.Merijn
        I found a solution, I will use:
        my ($name, $major, $minor, $build, $id) = Win32::GetOSVersion();
        and check the values are greater than or equal to id - 2, $major - 6, $minor - 1. I guess higher version of Windows got the 262 value