VBScript to determine OS Version and Service Pack

July 21, 2009
Filed Under VBScript 

Microsoft Windows LogoThe next few posts will all revolve around using vbscript as a solution to common tasks/needs while working in a Windows Server environment.  These scripts were created to help make my job easier.

The first script I want to share demonstrates how to poll Active Directory and return all computers and their Operating System and service pack.  My specific need was to find all computers not running XP Service Pack 3. The general idea for this script is to return what AD sees and place the output into a text file.  This script is very easily modified to suit many needs and simply uses ADO and an LDAP query to get the results.  It then uses FSO to place the results into a text file.

Option Explicit
' Declare some variables
Dim strFilePath, objFSO, objFile, adoConnection, adoCommand
Dim objRootDSE, strDNSDomain, strFilter, strQuery, adoRecordset
Dim strDN, strComputerName, strOS, strOSVer
Dim computerCount
Dim OUArray, i, strOU
' Define the path for our text file
strFilePath = "C:\log.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open the file for write access.
Set objFile = objFSO.OpenTextFile(strFilePath, 2, True, 0)
' Use ADO to search the domain for all users.
Set adoConnection = CreateObject("ADODB.Connection")
Set adoCommand = CreateObject("ADODB.Command")
adoConnection.Provider = "ADsDSOOBject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Determine the DNS domain from the RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
' Filter to retrieve all user objects.
strFilter = "(&(objectCategory=computer)(objectClass=computer))"
strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter _
    & ";distinguishedName,operatingsystem,operatingsystemservicepack,samaccountname;subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 190
adoCommand.Properties("Cache Results") = False
computerCount = 0
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
    strComputerName 	= replace(adoRecordset.Fields("samaccountname").Value,"$","")
    strOS 		= adoRecordset.Fields("operatingsystem").Value
    strOSVer		= adoRecordset.Fields("operatingsystemservicepack").Value
    strDN		= adoRecordset.Fields("distinguishedName").Value
    OUArray 		= split(strDN,",")
    strOU = ""
    ' Filter out just the Organizational unit of the computer
    For i = 0 to ubound(ouarray)
       If instr(ouarray(i),"OU=") > 0 Then
     	  strOU = strOU & "," & ouarray(i)
       End If
    strOU = mid(strOU,2,len(strOU))
    ' Determine if we find a computer that is running XP but does NOT have Service Pack 3
    If strOS <> "" and strOSVer <> "" Then    
      If strOS = "Windows XP Professional" and strOSVer <> "Service Pack 3" Then
        objFile.WriteLine strComputerName & " ; " & strOS & " ; " & strOSVer & " ; " & strOU
        computerCount = computerCount + 1
      End If
    End If
' Clean up.
Set objFile = Nothing
Set objFSO = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
Set objRootDSE = Nothing
Set adoRecordset = Nothing
Wscript.Echo "Done" & " - " & computerCount & " Records Found"

The output file will generate results that is semicolon delimited and will look like:

Computer Name; Operating System; Service Pack; Organizational Unit

Rating 5.00 out of 5


5 Responses to “VBScript to determine OS Version and Service Pack”

  1. hashir on September 14th, 2010 5:10 am


    This script is very nice..

    Could you please let me know what needs to be changed in the script to find the systems which is having Windows XP SP3.


  2. Bill Ritz on December 6th, 2010 12:41 pm

    Change the line that says:
    strOSVer <> “Service Pack 3”

    strOSVer = “Service Pack 3”

  3. sharath on May 2nd, 2011 9:56 am

    hi ,

    should we give domain admin credentials for this to work ?

  4. Amanasan on August 23rd, 2013 1:36 pm

    When I run the script it make the grinding noise on my PC now hard drive seem to be not working so well. Please add code to show how make the grinding noise no more. I fear for my computer after this.

  5. Mario on January 26th, 2015 11:25 am

    I am looking to get all 32Bit OS machines How do I get that accomplish with your script

Leave a Reply

Recent Posts

Microsoft Outlook PST large file size and not compacting
Installing Asterisk on Ubuntu 10.10 as Voicemail for Avaya S8700/8300 (Part 2)
Preparing for Asterisk on Ubuntu 10.10 as Voicemail for Avaya S8700/8300 (Part 1)
Linux Updates from Command Line
NSClient Error – Could not get data for
Gmail Notifier not working
Windows cannot access the specified device
The remote server has been paused or is in the process of being started.
APPCMD – HTTP 503 – Service Unavailable Windows 2008
Firefox Address Bar Not Changing in Ubuntu
Running Internet Explorer on Ubuntu
Could not create a Disk Cache Sub-directory for the Application Pool.
Event ID 13 Source AutoEnrollment
VBScript to determine OS Version and Service Pack
Can’t find script engine VBScript
Blackberry automatically deleting messages
The version of the Active Directory schema of the source forest is not compatible with the version of Active Directory on this computer
Neat tip in WordPress
Utilizing the & symbol (ampersand) in Microsoft Access Labels
Firefox Plugins in Ubuntu not working – Java, Flash, etc
Failed to execute request because the App-Domain could not be created
How to uninstall Windows Messenger
Disassociating Word from Internet Explorer
The test form is only available for requests from the local machine
WordPress Plugin: Current Read Widget
Disable RSS feeds in WordPress
The Requested Theme Does Not Exist Error in WordPress
HP Printer gets ip address of
Error opening tftp://
Set a Link Button as default on Enter press
Ajax + IE = Unknown runtime error
PNG Files Do Not Show Transparency in IE6
Automatically Log In To Ubuntu
Using wuauclt and known switches
Prompted to Install mimeclnt.msi when Using OWA
Nagios Service Check Timed Out
How to disable ETags in IIS6
Customizing the WordPress “Read More” link
Using Tasklist.exe to view running programs
How to create a Batch file to download FTP files
How to add an Expires Header in IIS
Blinking Amber Light on Dell Server
How to reset a Cisco 2924 enable password
Enabling HTTP Compression in IIS6
Common CDP (Cisco Discovery Protocol) Problems
Resetting MYSQL Root Password in Ubuntu

Join the mailing list

Check your email and confirm the subscription