Skip to content

How to Enable or Disable a Windows Optional Feature in Batch Script

Learn how to automate Windows optional feature management (Telnet Client, WSL, Hyper-V, IIS) using Batch scripts and DISM commands for standardized, secure, and fast system configuration.

Windows comes with a variety of built-in components, like the Telnet Client, Hyper-V, or the Windows Subsystem for Linux (WSL), that are turned off by default to save resources and improve security. Manually navigating through the "Turn Windows features on or off" menu is slow and prone to error, especially when setting up multiple environments. By using a Batch script to enable or disable these features, you can automate your system configuration, ensuring that every machine has the exact tools it needs.

This guide demonstrates how to use the DISM tool to manage these features with precision.

Why Automate Feature Management?

  • Standardization: Ensuring every developer on your team has WSL2 or Containers enabled.
  • Security: Proactively disabling legacy, vulnerable features like SMB 1.0/CIFS File Sharing Support across all devices.
  • Speed: Installing complex dependencies like IIS (Internet Information Services) in seconds rather than minutes.

The Core Command: DISM

The Deployment Image Servicing and Management (DISM.exe) tool is the standard utility for managing Windows features from the command line.

  • To Enable: dism /online /enable-feature /featurename:<Name> /all /norestart
  • To Disable: dism /online /disable-feature /featurename:<Name> /norestart

Important Flags

  • /online: Tells DISM to work on the currently running OS.
  • /featurename: The internal name of the feature.
  • /all: Automatically enables any "parent" features required for the target to work.
  • /norestart: Prevents Windows from automatically rebooting immediately (allowing your script to finish).

Creating a Feature Management Script

The following script provides a simple menu to either enable or disable a specific feature (in this example, the Telnet Client).

@echo off
setlocal EnableDelayedExpansion

set "FEATURE=TelnetClient"

echo ============================================================
echo   Windows Optional Feature Manager
echo ============================================================

:: 1. Check for Admin Rights
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Please run this script as Administrator.
    pause
    exit /b 1
)

echo Target Feature: %FEATURE%
echo.
echo [1] Enable Feature
echo [2] Disable Feature
echo [3] Exit
echo.
set /p "CHOICE=Select an option (1-3): "

if "!CHOICE!"=="1" goto :EnableFeature
if "!CHOICE!"=="2" goto :DisableFeature
if "!CHOICE!"=="3" exit /b 0

echo [ERROR] Invalid option selected.
pause
exit /b 1

:EnableFeature
echo [PROCESS] Enabling %FEATURE%...
:: We use /all to catch dependencies
dism /online /enable-feature /featurename:%FEATURE% /all /norestart
goto :Result

:DisableFeature
echo [PROCESS] Disabling %FEATURE%...
dism /online /disable-feature /featurename:%FEATURE% /norestart
goto :Result

:Result
if !errorlevel! equ 0 (
    echo [SUCCESS] Operation completed successfully.
    echo [NOTE] You may need to restart your computer for changes to take effect.
) else (
    echo [ERROR] Operation failed. Code: !errorlevel!
)
pause
exit /b

Common Pitfalls and How to Avoid Them

Using the Wrong Feature Name

DISM requires the "Internal Name," which is often different from the "Friendly Name" seen in the Control Panel.

Wrong Way:

dism /online /enable-feature /featurename:"Internet Information Services"
:: Result: "Feature name not found."

Correct Way: Find the real name first by running dism /online /get-features /format:table. For IIS, the internal name is IIS-WebServerRole.

Forgetting Dependencies

Some features won't start if their parent components are missing.

Best Practices for Successful Deployment

  1. Check Status First: Always use the /get-featureinfo command before enabling/disabling to see if the feature is already in the desired state.
  2. Handle Reboots Gracefully: Most OS changes require a reboot. Instead of letting DISM force a reboot, use /norestart and then add a clean shutdown /r command at the end of your script after checking the user's readiness.
  3. Check Feature Availability: Some features are only available on specific Windows Editions (e.g., Hyper-V is not available on Home edition). DISM will report "Feature name not found" if the edition doesn't support that component.

Conclusion

Enabling or disabling Windows optional features via Batch script is a critical skill for any system administrator or power user looking to automate their workstation setup. By leveraging the DISM utility, you gain a reliable, repeatable way to customize your OS without the need for manual navigation. Whether you are stripping out legacy components for security or building a high-performance development environment, this automated approach ensures that your system features are always exactly where they need to be, providing a stable and efficient platform for your specific workflows.

Navigation