Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Use PowerShell and WMI to Glean Basic Operating System Information

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to obtain basic operating system information.

Hey, Scripting Guy! Question Hey, Scripting Guy! I need to check on computers on my network to find out the operating system, service pack level, and whether the computer has been rebooted in the last 30 days. Can you help with this?

—DD

Hey, Scripting Guy! Answer Hello DD,

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife is starting to get excited about the MVP Summit in Redmond. She was out there a few years ago with me, and she loved the chance to meet so many MVPs from all over the world. This is her first year going as an actual MVP, and she is heading there without me.

I have been talking with the Windows PowerShell team, and I have seen the list of sessions that they are planning. It looks like it will be awesome. That is key. Is it something you want to do? Is it something you would want to go to? The answer of course is, “Yes!”

I am hoping the Scripting Wife will write at least one blog post about her experience attending her first MVP Summit as an MVP. I do know that she has gotten together with a fellow MVP from the Charlotte Windows PowerShell User Group, and that they are planning something. But that is NDA (and she says I do not have the need to know). Stay tuned…

Basic operating system information?

One of the easiest things to do is to use WMI to obtain some basic operating system information. I only need to remember two things: use the Get-CimInstance cmdlet and specify the Win32_OperatingSystem WMI class. That is it. Here is the command and the output:

PS C:\> gcim Win32_OperatingSystem

SystemDirecto Organization  BuildNumber  RegisteredUs SerialNumber Version    

ry                                       er                                   

------------- ------------  -----------  ------------ ------------ -------    

C:\WINDOWS...               9600         Windows User 00260-300... 6.3.9600    

The most important information from this output is the build number and the version. Actually, the version is all I need these days. The build is 9600 and the version is 6.3.9600. So that is major version 6, minor version 3, and build 9600 (which ends up being Windows 8.1).

There is, of course, more information available from this WMI class—a lot more. To get that, I pipe the resulting object to the Format-List cmdlet and choose to display all of the properties. Here is the output:

PS C:\> gcim Win32_OperatingSystem | fl * 

Status                                    : OK

Name                                      : Microsoft Windows 8.1 Pro with Media Center|C:\WINDOWS|\Device\Harddisk0\Partition4

FreePhysicalMemory                        : 9582612

FreeSpaceInPagingFiles                    : 1099776

FreeVirtualMemory                         : 10127712

Caption                                   : Microsoft Windows 8.1 Pro with Media Center

Description                               :

InstallDate                               : 2/2/2014 9:53:35 PM

CreationClassName                         : Win32_OperatingSystem

CSCreationClassName                       : Win32_ComputerSystem

CSName                                    : EDLT

CurrentTimeZone                           : -240

Distributed                               : False

LastBootUpTime                            : 9/5/2014 5:01:06 PM

LocalDateTime                             : 9/5/2014 5:46:32 PM

MaxNumberOfProcesses                      : 4294967295

MaxProcessMemorySize                      : 137438953344

NumberOfLicensedUsers                     :

NumberOfProcesses                         : 90

NumberOfUsers                             : 4

OSType                                    : 18

OtherTypeDescription                      :

SizeStoredInPagingFiles                   : 1099776

TotalSwapSpaceSize                        :

TotalVirtualMemorySize                    : 17691556

TotalVisibleMemorySize                    : 16591780

Version                                   : 6.3.9600

BootDevice                                : \Device\HarddiskVolume2

BuildNumber                               : 9600

BuildType                                 : Multiprocessor Free

CodeSet                                   : 1252

CountryCode                               : 1

CSDVersion                                :

DataExecutionPrevention_32BitApplications : True

DataExecutionPrevention_Available         : True

DataExecutionPrevention_Drivers           : True

DataExecutionPrevention_SupportPolicy     : 2

Debug                                     : False

EncryptionLevel                           : 256

ForegroundApplicationBoost                : 2

LargeSystemCache                          :

Locale                                    : 0409

Manufacturer                              : Microsoft Corporation

MUILanguages                              : {en-US}

OperatingSystemSKU                        : 103

Organization                              :

OSArchitecture                            : 64-bit

OSLanguage                                : 1033

OSProductSuite                            : 256

PAEEnabled                                :

PlusProductID                             :

PlusVersionNumber                         :

PortableOperatingSystem                   : False

Primary                                   : True

ProductType                               : 1

RegisteredUser                            : Windows User

SerialNumber                              : 00260-30060-23410-AB864

ServicePackMajorVersion                   : 0

ServicePackMinorVersion                   : 0

SuiteMask                                 : 272

SystemDevice                              : \Device\HarddiskVolume4

SystemDirectory                           : C:\WINDOWS\system32

SystemDrive                               : C:

WindowsDirectory                          : C:\WINDOWS

PSComputerName                            :

CimClass                                  : root/cimv2:Win32_OperatingSystem

CimInstanceProperties                     : {Caption, Description, InstallDate, Name...}

CimSystemProperties                       : Microsoft.Management.Infrastructure

                                            .CimSystemProperties

Now I have basic information and extended information. But the requirements are pretty specific: Operating System, Service Pack level, and whether rebooted in the last 30 days. I could do this at the Windows PowerShell console, but I may as well fire up the Windows PowerShell ISE and do it there. It will be neater.

First use CIM

The first thing I am going to do is use the CIM cmdlets to retrieve an instance of the Win32_OperatingSystem class. Here is the line of code that does that:

Get-CimInstance Win32_OperatingSystem 

Now I need to select the appropriate properties. I do this by using the Select-Object cmdlet as shown here:

Select-Object Name, Version, ServicePackMajorVersion,

The tricky part is determining if the computer has been up for 30 or more days. To do this, I look at the LastBootupTimeproperty, and I compare it with the DateTime object that is returned by Get-Date. To get 30 days, I use the New-TimeSpan cmdlet. I put all of this into a hash table so I can create a custom property on my returned object. Here is the script:

@{Label='RebootInLast30Days';

     Expression={((Get-Date) - $_.lastbootuptime) -ge (New-TimeSpan -Days 30)}}

The complete script is shown here:

Get-CimInstance Win32_OperatingSystem |

Select-Object Name, Version, ServicePackMajorVersion,

@{Label='RebootInLast30Days';

     Expression={((Get-Date) - $_.lastbootuptime) -ge (New-TimeSpan -Days 30)}}

When I run the script, the following output is produced:

Image of command output

DD, that is all there is to using WMI to obtain operating system information. WMI Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>