The one constant about Windows operating systems is that there is always an update. Major updates are delivered as service packs. In fact, some people won’t even upgrade to a new operating system until the first service pack has been released. For the rest of us, and those of you who wait until SP1, you probably need a way to get a handle on what OS service packs you have installed. Assuming you don’t have some sort of system or enterprise management solution in place, you can gather this information yourself pretty easily.
Service pack information is stored in WMI as part of the Win32_OperatingSystem class. So to find out what service pack a particular computer is running, all you need to do is query WMI, and this doesn’t have to require complex scripting. When you query WMI, you will get an instance back of the Win32_OperatingSystem class. The properties we are most interested in are ServicePackMajorVersion and ServicePackMinorVersion, although I can’t think of an incremental OS service pack. The other useful property is CSDVersion, which displays service pack information in a “friendly” format. First, let me show you how to gather this information using the command line tool, WMIC.EXE.
C:\>wmic os get servicepackmajorversion,csdversion CSDVersion ServicePackMajorVersion Service Pack 1 1
This command queried the local computer for the 2 properties. Querying a few remote machines isn’t much more work.
C:\>wmic /node:quark,'jdhit-dc01' os get servicepackmajorversion,csdversion,csname,caption /format:list Caption=Microsoft Windows 7 Professional CSDVersion=Service Pack 1 CSName=QUARK ServicePackMajorVersion=1 Caption=Microsoft(R) Windows(R) Server 2003, Enterprise Edition CSDVersion=Service Pack 2 CSName=JDHIT-DC01 ServicePackMajorVersion=2
I included a few other properties so I could see the computername and the operating system. One quick tip: when the computername has a dash, enclose it in quotes as I had to do. I don’t want to turn this into a WMIC lesson, but as I hope you can see, it is pretty easy to use and discover what service pack is installed.
Using Windows PowerShell is just as easy. The WMI class and property names are the same.
PS C:\> Get-WmiObject win32_operatingsystem -comp "jdhit-dc01","quark" | Select CSName,ServicePackMajorVersion,Caption | format-list CSName : JDHIT-DC01 ServicePackMajorVersion : 2 Caption : Microsoft(R) Windows(R) Server 2003, Enterprise Edition CSName : QUARK ServicePackMajorVersion : 1 Caption : Microsoft Windows 7 Professional
It doesn’t take much more effort to query hundreds of computers to find those that are not up to date. In fact, I wrote a PowerShell function called Test-ServicePack that you can download from the site. By default, this function will test a computer and see if its installed service pack is at least a specific version.
PS C:\> test-servicepack "jdhit-dc01" -Version 2 True
To see more information use the –Detail parameter.
PS C:\> test-servicepack "jdhit-dc01" -Version 2 -Detail Computername : JDHIT-DC01 OperatingSystem : Microsoft(R) Windows(R) Server 2003, Enterprise Edition ServicePackMajorVersion : 2 ServicePack : Service Pack 2 OK : True
The function tests if the service pack is at least a certain version. My domain controller is at SP 2, but checking for SP1 will return True.
>PS C:\> test-servicepack "jdhit-dc01" -Version 1 True
To test for an exact version use the –Exact parameter.
PS C:\> test-servicepack "jdhit-dc01" -Version 2 -Exact True PS C:\> test-servicepack "jdhit-dc01" -Version 3 -Exact False
To put this in a larger perspective, let’s say Microsoft releases a new service pack and you need a list of computers that will need it. Using a text file of your servers, it is as easy as this:
PS C:\> get-content computers.txt | test-servicepack -version 2 -detail -exact| Where {!$_.OK} | Select Computername,ServicePackMajorVersion | Out-file c:\work\NeedSP2.txt
I also could have searched for version 1 only.
Finding what OS service packs you have installed isn’t difficult. Service pack information is not hidden away, you just need to know where to look.