March 27th, 2015

Use PowerShell to Remove Optional Features from Windows VHD

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to remove optional features from Windows virtual hard disks.

Hey, Scripting Guy! Question Hey, Scripting Guy! I have a number of virtual machines that have optional features that they do not need, such as Windows Media Player. In fact, the virtual machines are set up with no audio, so having Windows Media Player is sort of a waste. How can I easily remove this feature?

—CG

Hey, Scripting Guy! Answer Hello CG,

Microsoft Scripting Guy, Ed Wilson, is here. This morning started off early. IIS MVP, Teri Donahue, stopped by to pick up Teresa’s Surface Pro 2. Teri and Teresa are meeting at an MVP conference in Philly. I had to get a new SIM card for the Surface because the dial-up adapter quit working, and Teri is taking it to Teresa, who is pretty much going through withdraw without her device.

Not that she is completely deviceless, she had my Surface Pro 2 and her original Surface RT with her, not to mention her Windows Phone, but still she was jonesing without her favorite device. Thanks Teri! Anyway, I took the early morning wake up to head to the gym to get my workout over so I could get back to work answering email sent to scripter@microsoft.com.

CG, speaking of withdrawl…

It is actually pretty easy to use Windows PowerShell to remove unneeded features from your VHDs. (This technique also works for WIM images.)

One of the cool things about using the DISM cmdlets is that it permits me to work with optional features in a supportable and reliable method. In the old days, I had to worry about dependencies and hope that I did not break things when I used tools that were designed to shrink Windows images, but that is no longer the case.

The first thing I do is mount an image that I want to work with to a folder. Here is the code I use:

$image = "E:\vms\vhd\c1\c1.vhdx"

$path = "c:\image"

 MD $path

 Mount-WindowsImage -ImagePath $image -Path $path -Index 1

After I have my virtual machine mounted to a folder, I use the Get-WindowsOptionalFeature to look at the optional features:

Get-WindowsOptionalFeature -Path $path

The following image shows the output from this command:

Image of command output

To find related features, use wildcard characters. The following command illustrates finding media-related features:

PS C:\> Get-WindowsOptionalFeature -Path C:\image -FeatureName *media*

Feature Name      : MediaPlayback

Display Name      : Media Features

Description       : Controls media features such as Windows Media Player and Windows Media Center.

Restart Required  : Possible

State             : Enabled

Custom Properties :

                    \SoftBlockLink : http://go.microsoft.com/fwlink?LinkID=153156        

Feature Name      : WindowsMediaPlayer

Display Name      : Windows Media Player

Description       : Windows Media Player

Restart Required  : Possible

State             : Enabled

Custom Properties :

                    \SoftBlockLink : http://go.microsoft.com/fwlink?LinkID=140092

To remove an optional Windows feature, I pipe the results of the Get-WindowsOptionalFeature cmdlet to the Disable-WindowsOptionalFeature cmdlet. I also use the –Remove switch to actually remove the feature and not simply disable the feature. This command is shown here (this is a single-line command that wraps at the pipe character for readability):

Get-WindowsOptionalFeature -Path C:\image -FeatureName *media* |

Disable-WindowsOptionalFeature -Remove

Now, I put everything together into a script:

Push-Location

Set-Location c:\

WorkFlow Service-Image

{

 $image = "E:\vms\vhd\c1\c1.vhdx"

 $path = "c:\image"

 MD $path

  Sequence {

   Mount-WindowsImage -ImagePath $image -Path $path -Index 1

   Get-WindowsOptionalFeature -Path C:\image -FeatureName *media* |

   Disable-WindowsOptionalFeature -Remove

   Dismount-WindowsImage -Path $path -Save

   RD $path -Force

 } }

Service-Image

Pop-Location

When I run the script, the following output appears:

Image of command output

CG, that is all there is to using Windows PowerShell to remove optional features from virtual machines. Join me tomorrow when I will have a guest blog post by Windows PowerShell MVP, Teresa Wilson. She will talk about plans for the Scripting Guys Ignite booth.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.