Skip to main content

How to Check if a Windows Optional Feature is Enabled or Disabled in Batch Script

Windows Optional Features (such as Hyper-V, WSL, Telnet Client, or the OpenSSH Server) allow you to customize your OS for specific workloads. For developers and system administrators, verifying that these features are active is a common prerequisite for running complex automation or specialized software. Using the Deployment Image Servicing and Management (DISM) tool within a Batch script, you can automatically query the status of any feature.

This guide will show you how to identify enabled features and build logic to handle those that are missing.

Why Check for Optional Features?

  • Dependency Management: Ensuring Hyper-V is enabled before attempting to run Docker or a Virtual Machine.
  • Security Auditing: Verifying that legacy, insecure features like SMB1 are disabled across your network.
  • Workflow Automation: Automatically enabling the Telnet or OpenSSH client if a technician needs it for a specific task.
Terminology: Feature Name

Windows uses specific internal names for features (e.g., Microsoft-Windows-Subsystem-Linux) which are different from their "Friendly Names" in the Control Panel. You must use the internal name in your script.

Core Command: DISM /Get-Features

The dism utility is the professional standard for managing Windows features. To check a status, we use the /Get-Features or /Get-FeatureInfo switch.

Listing All Features

@echo off
:: This exports a list of all features and their states to a text file
dism /online /get-features /format:table > FeatureList.txt

Creating a Feature Validation Script

A robust script should target a specific feature, check its state, and provide clear feedback or take action if the feature is disabled.

@echo off
setlocal EnableDelayedExpansion

:: Define the internal name of the feature
:: Example: "TelnetClient" or "Microsoft-Hyper-V-All"
set "FEATURE_NAME=TelnetClient"

echo ============================================================
echo Windows Optional Feature Validator
echo ============================================================

:: 1. Check for Administrative Privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
pause
exit /b 1
)

echo [PROCESS] Checking status for: %FEATURE_NAME%...

:: 2. Use DISM to query the specific feature
:: We search for "State" and "Enabled" appearing on the same line
dism /online /get-featureinfo /featurename:%FEATURE_NAME% 2>nul | findstr /i /c:"State" | findstr /i /c:"Enabled" >nul

if %errorlevel% equ 0 (
echo [SUCCESS] %FEATURE_NAME% is currently ENABLED.
) else (
echo [WARNING] %FEATURE_NAME% is DISABLED or not installed.

:: 3. Optional: Ask the user to enable it
set /p "CHOICE=Would you like to enable this feature now? (Y/N): "
if /i "!CHOICE!"=="Y" (
echo [PROCESS] Enabling %FEATURE_NAME%...
dism /online /enable-feature /featurename:%FEATURE_NAME% /all /norestart
)
)

echo ============================================================
pause

Common Pitfalls and How to Avoid Them

Incorrect Feature Names

Feature names are case-sensitive in some versions of DISM and must be precise.

Wrong Way:

dism /online /get-featureinfo /featurename:"Virtual Machine Platform"
:: This is a friendly name. DISM will return "Feature name not found."

Correct Way: Always verify the internal name first by running dism /online /get-features. For example, the "Virtual Machine Platform" is actually named VirtualMachinePlatform.

DISM vs. PowerShell

In Windows 10/11, you can also use Get-WindowsOptionalFeature. While powerful, calling PowerShell from Batch adds a layer of complexity (and potential execution policy blocks).

SEO and UX Tip

If you are writing a script for a "clean" machine where you aren't sure if PowerShell permissions are set, DISM is the safer, more compatible choice as it is a native .exe.

Best Practices for Feature Management

  1. Check for "Staged" state: Sometimes a feature is "staged" but not enabled. DISM will report this. A staged feature is ready to be enabled without needing an internet connection.
  2. Combine with Reboots: Many features (like Hyper-V) require a system restart to finalize. Your script should detect if a reboot is pending:
    :: Check for a pending reboot via the registry
    reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" >nul 2>&1
    if %errorlevel% equ 0 echo REBOOT REQUIRED!
  3. Logs: If you are deploying features via a script, redirect the DISM output to a log file for later auditing.
Dependencies

Some features have "Parent" dependencies. When enabling a feature via your script, always add the /all flag to ensure any required parent features are also enabled automatically.

Conclusion

Detecting the status of Windows Optional Features via Batch script is an essential practice for ensuring your environment is correctly configured for your tasks. By leveraging the DISM utility to programmatically check for enabled or disabled states, you can build intelligent automation that self-corrects or warns users when critical dependencies are missing. This professional approach to system configuration ensures that your tools work reliably and that your system remains optimized with only the necessary features active, maintaining both performance and security.