Adding roles and features

You can use DISM to enable or disable Windows roles and features and also customize Windows images. It is a very convenient feature that enables you to deploy computers with pre-installed roles and features, thus avoiding the inconvenience of installing roles and features and restarting a server after server deployment.

You have to identify which features are available in the mounted image and the exact name of the feature or a role you want to add, using either /Get-Features or Enable-WindowsOptionalFeature in DISM and PowerShell, respectively.

To identify which roles and features are available in the image mounted in c:\mount using dism.exe, use the following command:

DISM.exe/image:c:\mount /Get-Features

This produces the following output:

Alternatively, use the following PowerShell command:

Get-WindowsOptionalFeature -Path C:\mount

This produces exactly the same output, as shown in the following screenshot:

Some PowerShell command alternatives produce the same output as dism.exe commands and some PowerShell commands generate a more informative output. Both alternatives accomplish the same results; it is up to your personal preference as to which commands to use.

To enable a specific feature in the image, such as IP Address Management (IPAM), use the following dism.exe command:

DISM.exe /image:c:\mount /Enable-Feature /FeatureName:IPAMServerFeature

To enable all of the parent features of the specified feature, use the /all option:

DISM.exe /image:c:\mount /Enable-Feature /all /FeatureName:Microsoft-Hyper-V

Sometimes, you want to disable a feature you no longer need or support. To disable a feature in an image, use the Disable-Feature option in DISM or the Disable-WindowsOptionalFeature command in PowerShell, as shown in the following example:

DISM
DISM.exe /image:c:\mount /Disable-Feature /FeatureName:RASRoutingProtocols

PowerShell
Disable-WindowsOptionalFeature -Path c:\mount -FeatureName RASRoutingProtocols -Remove

When you are finished servicing an image, or you want to service a different image, you have to unmount the image. You have to specify whether you want to save or discard these changes using the /commit or /discard options.

To unmount an image that's mounted to the c:\mount folder and save your changes, use the following code:

DISM
DISM.exe /unmount-wim /mountdir:c:\mount /commit
or
DISM.exe /unmount-Image /mountdir:c:\mount /commit

PowerShell
Dismount-WindowsImage -Path C:\mount -Save

A list of the DISM command syntaxes we have discussed are shown as follows:

  • The following command displays image information:
DISM.exe /Get-ImageInfo /ImageFile:<path_to_image.wim> [{/Index:<Image_index> | /Name:<Image_name>}]
  • The following command mounts an image:
DISM.exe /Mount-Image /ImageFile:<path_to_image_file> {/Index:<image_index> | /Name:<image_name>} /MountDir:<path_to_mount_directory> [/ReadOnly] [/Optimize] [/CheckIntegrity]
  • The following command services an image offline:
DISM.exe /image:<path_to_image_directory> [/Get-Drivers | /Get-DriverInfo | /Add-Driver | /Remove-Driver | /Export-Driver]
  • The following command services an image online:
DISM.exe /Online [/Get-Drivers | /Get-DriverInfo | /Export-Driver]
  • The following command enables features in an image:
DISM.exe /Enable-Feature /FeatureName:<name_in_image> [/PackageName:<name_in_image>] [/Source: <source>] [/LimitAccess] [/All]
  • The following command disables features in an image:
DISM.exe /Disable-Feature /FeatureName:<name_in_image> [/PackageName:<name_in_image>] [/Remove]
  • The following command unmounts an image:
DISM.exe /Unmount-Image /MountDir:<path_to_mount_directory> {/Commit | /Discard} [/CheckIntegrity] [/Append]

The list of DISM command-line switches and their respective descriptions are shown in the following table: