PowerShell: How to clean up the WinSxS folder in Windows Server 2012 R2

Windows Server 2012 R2

In this post we’ll look at how to use PowerShell to reduce the size of the WinSxS folder in Windows Server 2012 R2.

A customer has a very quick SSD based server at a cloud provider, but although it is SSD based it only has a tiny 40GB C:, which is a very small footprint for the OS, a couple of apps and logs files. So i was asked to take a look and see what i could do to make a bit of room.

The WinSxS folder contains the files for all the Windows Features you can install in the default operating system. Each time you run a windows update files in the WinSxS folder get update and the size will continue to grow.

Since Windows Server 2012 Microsoft have made it very easy to tidy the WinSxS folder up. They introduced a new feature called “Features on Demand”. Rather than the WinSxS containing all the binaries for all the features you could possibly install on the server, “Features on Demand” allows you to remove the files for features you aren’t using.

If at a later date you want to install a feature you have removed from the WinSxS folder you’ll need to specify a location for the source files.

I used one of my favourite little tools to start off with to find out where all the data was being stored – WinDirStat. The image below is taken from a test server.

Looking at the the WinSxS folder in more detail it is taking up 7.3GB on disk, which is quite a chunk of space if you only have 40GB.

WinSxS before

So in PowerShell, running the command Get-WindowsFeature, lists all the features and displays the Name and Install state. From the image below you can see DC, DNS and basic File Services are installed, but the rest are available, meaning the binaries are in the WinSxS folder ready and waiting!

Get-WindowsFeatureTo uninstall a windows feature you use the command Uninstall-WindowsFeature -Name featurename -Remove but this is for an individual feature and would take an age to remove them that way.

So wouldn’t it be really cool if we could pass the output from Get-WindowsFeature where the Install State is available to the Uninstall-WindowsFeature cmdlet.

Well using a bit PowerShell piping we can (well sort of). So rather than use Install State is available we’ll use Installed -Eq $FALSE,

So the command we are after is:

Get-WindowsFeature | Where-Object -FilterScript {$_.Installed -Eq $FALSE} | Uninstall-WindowsFeature

Get-WindowsFeature Uninstall-WindowsFeature

Then if we run the just the Get-WindowsFeature cmdlet.

Get-WindowsFeature | WhereObject -FilterScript {$_.Installed -Eq $FALSE}

You can see that the Install State is now Removed.

Get-WindowsFeature removedSo how much space did we save?

WinSxS after

In this instance we saved about 2.3GB so we reduced the WinSxS folder by almost a third, which isn’t bad going for a relatively new server.

Source Articles:

Technet – Ask PFE



Related Articles:

1. How to clean up WinSxS folder on SBS 2011 or Windows 2008 R2

2. How to upgrade Windows Server 2012 R2 evaluation version to full version

3. How to install Exchange 2013 (SP1) on Windows Server 2012 R2

4. How to reset the Directory Services Restore Mode (DSRM) password

1 thought on “PowerShell: How to clean up the WinSxS folder in Windows Server 2012 R2

  1. Ian M

    Error in the above (missing -Remove in the command line text, OK in the screenshot):

    So the command we are after is:

    Get-WindowsFeature | Where-Object -FilterScript {$_.Installed -Eq $FALSE} | Uninstall-WindowsFeature -remove

Comments are closed.