Tuesday, January 22, 2013

How to NTFS compress Windows WinSxS folder

You may have an older computer, a smaller SSD or a VM with little space left on the system partition, and you need a couple of GBs of disk quickly. You did all the clean-up you could with Windows Disk Clean-Up or CCleaner, then you checked what's using most your disk with WinDirStat or TreeSize Personal, and now you may be thinking that WinSxS folder is quite large... I don't know about cleaning it up, but you can definitely try compressing it at filesystem level.

Before we go on, now it might be a good time to do that system partition backup you've been putting off for so long, just in case.

1. Start a Command Prompt (cmd.exe) as Administrator, in which to run the commands below.

2. Stop and disable Windows Installer and Windows Module Installer services (see sc):
sc stop msiserver
sc stop TrustedInstaller

sc config msiserver start= disabled
sc config TrustedInstaller start= disabled

3. Backup ACLs for WinSxS folder (see icacls), so we can restore later:
icacls "%WINDIR%\WinSxS" /save "%WINDIR%\WinSxS.acl" /t

[...]
Successfully processed 44231 files; Failed processing 0 files

4. Take ownership of WinSxS and subfolders (see takeown):
takeown /f "%WINDIR%\WinSxS" /r

SUCCESS: The file (or folder): "C:\Windows\WinSxS" now owned by user "COMPUTER\User".
[...]
SUCCESS: The file (or folder): "C:\Windows\WinSxS\x86_xnacc.inf_31bf3856ad364e35_6.1.7600.16385_none_b381dfe1d4da7da9\xnacc.sys" now owned by user "COMPUTER\User".

5. Grant full rights on WinSxS to your user:
icacls "%WINDIR%\WinSxS" /grant "%USERDOMAIN%\%USERNAME%":(F) /t

processed file: C:\Windows\WinSxS
[...]
Successfully processed 44231 files; Failed processing 0 files

6. Compress WinSxS files and sub-folders (see compact).

Some files might still be in use by say Explorer, you can see that by running the command without /i parameter so that is stops on first file in use and then use CTRL+F in Process Explorer to search for the file name. It should compress most of the files anyways, so we'll ignore those.
compact /s:"%WINDIR%\WinSxS" /c /a /i *

[...]
43653 files within 8596 directories were compressed.
6,021,146,695 total bytes of data are stored in 4,102,382,853 bytes.
The compression ratio is 1.5 to 1.

7. Restore ownership back to TrustedInstaller:
icacls "%WINDIR%\WinSxS" /setowner "NT SERVICE\TrustedInstaller" /t

processed file: C:\Windows\WinSxS
[...]
Successfully processed 44231 files; Failed processing 0 files

8. Restore ACLs for WinSxS folder we saved earlier.

Notice the command line doesn't specify WinSxS folder, but its parent (%WINDIR%) - it's because icacls stores paths starting with WinSxS in the .acl file; open up the file with Notepad or see more here.
icacls "%WINDIR%" /restore "%WINDIR%\WinSxS.acl"

processed file: C:\Windows\WinSxS
[...]
Successfully processed 44231 files; Failed processing 0 files

9. Restore Windows Installer and Windows Module Installer services:
sc config msiserver start= demand
sc config TrustedInstaller start= demand

* * *

Original (before)
Compressed (after)


12 comments :

Todd G. said...

This is an excellent write-up. Thank you for the time in putting it together.

We are working on a Windows 7 Embedded image for thin clients, and utilized this technique on a series of Windows folders including the WinSxS folder. We were able to reclaim 700 MB from the image. May not sound like a lot, but on a 4 GB DOM, this is HUGE.

Thank you again!

Dan Dar3 said...

Thanks Todd, glad you found it useful! interesting scenario.

I used to do this manually every now and then on my computers and various VMs, sometimes breaking it (http://dandar3.blogspot.com/2012/07/case-of-breaking-winsxs-folder-security.html).

Sounds like you needed a scripted / repeatable set of steps more than I did.

tralam said...

Very nice! That 'size on disk' for my WinSxS folder on a fully updated Win8 Pro install went from 10gb to around 6gb!

I wonder if this is also effective with installs which are sysprep'd and captured (whether final image size will be reduced or not).

Anyway, thanks so much for your article, how cool ;)

Pedro said...

I just wanted to stop by and thank you for this excellent post.

On my 5 year old Vista SP2 laptop, with tons of software and updates, compressing the WinSXS and Installer directories under c:\Windows saved me a full 11 GB!

I loved the detail and readability of your post, learned a few tricks (saving ACL's to a file, for example), and I will now be recommending this to anyone with Windows Vista, 7 or 8, and short of disk space.

Thanks and congratulations.

Dan Dar3 said...

Thanks Pedro,

appreciate the feedback and glad to hear it helped. I've been chasing this one for a while.

Anonymous said...

It's the best tutorial for compressing winsxs ever. It bugged me for years cuz I just can't get the damn right full rights...

Have to give you a thumb up sir!

mattyboy said...

Thanks man. This really needs to be more publicised more as it's a great tip! Worked well for me with Win8 on my full SSD.

dstruct2k said...

I would strongly advise AGAINST following these instructions on any machine with limited space. While each individual file is now compressed, this breaks every hardlink in WinSxS, meaning that the already-present method of space saving is totally ruined.

Yes, the total of all filesizes is smaller, but the truth is that many of these files are exact duplicates that are normally only stored once on the disk and hardlinked to multiple locations. Running the compress step breaks every hardlink and creates copies instead, which actually end up taking up more space than hardlinking, even when they're compressed.

Dan Dar3 said...

@dstruct2k
Thanks for your comment.

I'm not sure I agree though. If only limited free space is available, compact will fail for the file and it will just leave it as it was (uncompressed).

As to compress on hardlinks, I just ran a quick test and it appears the hardlinks are kept (Windows 8):

cd /d %temp%
fsutil file createnew file_a 1000000
fsutil hardlink create file_b file_a
compact file_* /c
fsutil hardlink list file_a

Pedro said...

I can't follow the logic of dstruct2k's comment either. If he was right, we would see an increase in disk usage after this procedure, or only slight reductions.

But that is not the case. I see huge reductions in disk usage. I just did this today on a Windows 7 running on a small (but fast!) 40 GB SSD hard disk. I won 6 GB. I know I was in desperate need for those gigabytes. I don't know if I need hard links at all...

@Dan Dar3:

I have a couple of suggestions:

1. Add the line
compact /s:"%WINDIR%\Installer" /c /a /i *
... to your procedure. It doesn't hurt, it is simpler to compress (no need to bother with ACL's), and gives you an extra GB or two.

2. Join everything together as a .cmd file for download.

I liked doing this step by step the first few times, watching everything, but now I am confident it works well and I would like to do it all in one take, to avoid all the long waits.

Anyway, I DO recommend to anyone backing up the system before attempting this - if anything goes wrong it could impair your OS. Once I have a backup, I prefer just running a .cmd file to do everything in one take.

Thanks once again!

Pedro said...

Another thing I suggest adding to the procedure: a "free-space defrag" is highly recommended as a final step. After thousands of file are compacted in-place, turning into smaller files, your disk is left like a swiss cheese.

I recommend Defraggler's free-space defrag (not the same thing as a simple defrag!).

If your disk is an SSD, however, it seems defragmentations provide no gains and can even be detrimental to disk health.

Dan Dar3 said...

Thanks Pedro, and I agree with you.

I usually have Defraggler configured on a weekly schedule (Settings > Options > Schedule) - defragging my workstation during lunch breaks or on servers / VMs on weekend nights. And yes, that is only a good idea with a spinning HDD, not SSDs as it will increase the (write) wear.

I wrote the post so everyone can see the "code" and thought if anyone finds to have to use it often they can put together as a script - I can look into providing it from a downloadable link, sure.

The %WINDIR%\Installer is also a good idea if you are looking to get some space back, there's definitely a lot more places where you can compress things to save space - I just focused on WinSxS here, as most of the other ones can be simply compressed through Windows Explorer UI.