Script to Determine Status of Optional Features Using Win32_OptionalFeature WMI Class

Windows 7 and Windows Server 2008 R2 include a new WMI class, Win32_OptionalFeature, that allows you to view the status of optional features in Windows (to query server role status, use Win32_ServerFeature).

The DISM command-line tool lets you view the same information about optional features using the command:

dism /online /get-features

You can also use DISM to check on the status of one specific optional feature. For example, to look at the status of NetFx2-Servercore:

C:\dism /online /get-featureinfo:NetFx2-ServerCore

Deployment Image Servicing and Management tool
Version: 6.1.7600.16385

Image Version: 6.1.7600.16385

Feature Information:

Feature Name : NetFx2-ServerCore
Display Name : Microsoft .NET Framework 2.0 ServerCore
Description : Microsoft .NET Framework 2.0 ServerCore
Restart Required : Possible
State : Disabled

Custom Properties:

(No custom properties found)

The operation completed successfully.

See Querying the Status of Optional Features on MSDN for equivalent PowerShell commands.

The following Vbscript code was generated using Scriptomatic Version 2.0. It will output the optional features and their status. 

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OptionalFeature", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
    WScript.Echo "Caption: " & objItem.Caption
    WScript.Echo "Description: " & objItem.Description
    WScript.Echo "InstallDate: " & WMIDateStringToDate(objItem.InstallDate)
    WScript.Echo "InstallState: " & objItem.InstallState
    WScript.Echo "Name: " & objItem.Name
    WScript.Echo "Status: " & objItem.Status
    WScript.Echo
Next

Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm: 
    WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
    Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
    & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

On a Server Core install of Windows Server 2008 R2, the Dfsradmin.exe command-line tool is installed as part of the DFSR-Infrastructure-ServerEdition optional feature. However if you attempt to run Dfsradmin and you have not also installed the .NET Framework 2.0 ServerCore feature you will receive the following error:

Application Error
The application was unable to start correctly (0xc0000135). Click OK to close the application.

Therefore if you want to script Dfsradmin commands, it can be helpful to first check to make sure the NetFx2-ServerCore feature is installed.

You can confirm an optional feature such as NetFx2-ServerCore is installed with a script such as this: 

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OptionalFeature", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objFeature In colItems
    If objFeature.Name = "NetFx2-ServerCore" And objFeature.InstallState = "1" Then
        blnNetFx2ServerCore = True
    End If
Next

If blnNetFx2ServerCore Then
    Wscript.Echo "NetFx2-ServerCore is installed."
Else
    Wscript.Echo "NetFx2-ServerCore is not installed."
End If