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 /c /s:"%WINDIR%\WinSxS" /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. Delete WinSxS.acl file (cleanup)
del "%WINDIR%\WinSxS.acl"

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

* * *

Original (before)
Compressed (after)


  1. 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!

  2. 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.

  3. 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 ;)

  4. 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.

  5. Thanks Pedro,

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

  6. 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!

  7. 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.

  8. 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.

  9. @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

  10. 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!

  11. 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.

  12. 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.

  13. Dan Dar3

    Thank you - really clear and simple to follow instructions that worked first time.


  14. Hi Dan

    This is an excellent idea and it work well upto restore ownership. But when I proceed with restore acl after executing thousands of files it got stopped. Originally there are 66000 files processed while restoring ownership but it get stopped at 12541 files while restore acl stating successfully processed 12541 files: failed processing 1 file

  15. What happens if you re-try the step? what was the message before "processed... failed..."? Antivirus?

  16. Same error while retrying also. Getting an access denied error for setbcdlocale.dll

  17. Right, can you check the security on the file (Explorer > Properties > Security). Then use Process Explorer > CTRL+F and search for the file and see if it's in use by what process.

  18. While I do have a backup, is there another clean method to revert to the previous state? something went wrong, got some messages about missing drivers while starting a game. I also noticed that my rather small windows partition was completely full, only 75Mb of free space left. It was OK after a reboot though. Great well-written and documented instruction. Good work.

  19. Thanks. I don't know what to say - you could use WinDirStat or similar to find out where the space is gone. I don't think drivers are stored in there. If you got a backup maybe you could compare against that and see what what got disturbed.

  20. I did all the steps again, this time uncompressing everything with the /u switch. The driver error message is gone, but what's more astonishing is that the free space is the same as with the compressed folder! I also noticed that some files in %windir% were compressed too, notepad.exe and a few others... I think something went really wrong, I'll restore my backup. Do you think it's a good idea to repeat these steps in 'safe mode'?

  21. Dan Dar 3.

    A big thanks for this.


    Just the thing I was looking for so I can grab as much shrinkage as possible for my fully set up Windows 7 install.wim file.

    I added the ability to run my made up cmd file to run as Administrator, then added taskkill explorer (to save tose vital few kb's!!) with start explorer at the end plus the installer folder tip from Pedro (cheers matey!!).

    Mananaged to get the install.wim below the magic threshold for a DVD. (yes, I do use a usb drive but old habits die hard!!)

    Again, a big THANKS!


  22. As an update to my last post.

    The installer compress saved 200mb on a fresh install (when I say fresh, Office was in but no Adobe Acrobat, Illustrator or Photoshop yet) and a tad short of 2GB on the Winsxs.

    These days 2GB is nothing, but up until 7 years ago I was running a very able XP Home system on a 20GB drive. Therefore I became a space pincher, and OCD at that!

    Now I have a few drives installed, some rescued from old laptops which are slow, but just used for document type stuff.

    To the point.

    When I first have to re-install Windows 7 (x64) I have a script that I run that deletes all the non English folders. It then deletes all the none Latin English fonts that, as a Brit, I'll never use.

    Whenever I do an sfc scannow run these all get replaced, which is no problem, I run my script again and they're gone (you ever tried moving from XP and searching the sytem32 folder on the eighth mouse scroll whereas in Windows 7 its the twenty fourth row because of all the foreign language folders??? Me index fingers worn out on the mouse wheel!!)

    So. To a question. What would be the best way to find all these language backups in Winsxs and when found would it be safe to treat them the same as I do in my running system?

    Opinion appreciated.

    As for the statement by dstruct2k. XP was always compressing things to save space.
    As far as I'm concerned the only time I need the Winsxs folder is when I (very rarely) would encounter a near BSOD experience.

    Just for fun, I just ran sfc /scannow on my soon to be replaced system drive after compressing winsxs folder.

    It may have took longer, it may not have, but I do know I only got the same three errors I always get before I discovered this wonderful tip.

    So, Dan Dar 3. Any tips on how to search/find all those "useless to me" language and font files?

    (I'm an amateur/no knowledge script writer, but I get there in the end!!)


    P.S. I'm going to spread the word on this blog. Great tip.

  23. Thanks Elmer, I'm glad you find the post useful.

    Send me an email with your delete script (dan.dar33 at gmail.com) or with details of what you want deleted and maybe we can figure out together what's the best way.

  24. @Dan Dar 3

    Email sent. "Elmer ~ Clear Winsxs of useless language (to me) files" from kvnjbll etc.

    TIA Kev

  25. Saved me about 4 GB from my massive 12.1 GB winsxs folder size.


  26. I won 2,5Gbytes storage of my valuable SSD 120Gb drive

    Excellent post, thank you

  27. Hi guys,

    If you get errors while restoring, add /C to the strings, it will force CMD to skip the error and process the rest of strings.

    icacls "%WINDIR%" /restore "%WINDIR%\WinSxS.acl" /C

    While it won't fix the error for that particular file, at least you won't get stuck.

    1. I needed to add this /C even when getting the permissions:

      icacls "%WINDIR%\WinSxS" /grant "%USERDOMAIN%\%USERNAME%":(F) /t /C

      Then I really got the permissions for most of the files. Without it I did not, because the command ended after setting permissions to 45 files.

  28. Here is the PowerShell code to accomplish this. Be sure to run PowerShell as admin and to adjust your execution policy (using Set-ExecutionPolicy) if needed.

    try {
    $winDir = "$($env:windir)"
    $winSxSDir = "$winDir\WinSxS"
    $aclPath = "$winDir\WinSxS.acl"
    $service1 = "msiserver"
    $service2 = "TrustedInstaller"

    Stop-Service $service1
    Stop-Service $service2

    Set-Service -Name $service1 -StartupType Disabled
    Set-Service -Name $service2 -StartupType Disabled

    icacls.exe $winSxSDir /save $aclPath /t

    takeown /f $winSxSDir /r

    icacls $winSxSDir /grant "$($env:USERDOMAIN)\$($env:USERNAME)":"(F)" /t

    compact /s:$winSxSDir /c /a /i *

    icacls $winSxSDir /setowner "NT SERVICE\TrustedInstaller" /t

    icacls $winDir /restore $aclPath

    Set-Service -Name $service1 -StartupType Automatic
    Set-Service -Name $service2 -StartupType Automatic
    catch { Write-Host -ForegroundColor Red $Error[0] }

  29. Hi David,
    thanks for your powershell script.
    I've attempted to run this against a 2008 server and a win7 laptop, but I get access denied during the compression process.
    The script does successfully apply the permissions there are no errors there. Any ideas what might be causing that?
    In my eyes the script looks correct, so I can't see what's causing the access denied.
    Would appreciate any advice you have, as I have a number of 2008 R2 servers missing SP1 and with a puny 25GB system disk.

  30. Thank you Dan for this excellent write-up. From a system admin who has struggled for years to find a proper way of reducing the size of the Winsxs folder; it worked well on Windows 2008R2.

  31. Thank you Tes, and thank you all for your feedback!

  32. Cannot stop or disable TrustedInstaller on Windows 8.1? Any idea how to fix this?

  33. @Jeff Savage
    You are running an elevated command line (Run As Administrator) correct? TrustedInstaller (Windows Installer) is usually stopped and started on demand, that's why I was thinking to stop it and disable it for the duration of the operation to avoid any inconsistencies. You could try without that step, but I would recommend doing a full system backup just in case other things might be going wrong.

  34. Shouldn't the big WinSxS.acl be deleted afterwards?

    1. @Bobzter
      And you're right, if everything worked fine we don't need the WinSxS.acl file anymore and I see it can be 40-50 MB. I've updated the steps to do that, thanks.

    2. Just to add that while trying things out I always kept the the .acl file on purpose, just in case things were failing I could simply redo some of the earlier steps (taking ownership, etc and then retrying to restore ACLs), but this is a "success" run so it won't be really needed.

  35. wow.. nice work !

  36. Hello Dan Dar3,
    and all the others of course...
    This might be a bit out-dated by now, but I thought I'd share anyway.

    I've been playing with the NTFS compression for some time because it bugged me the Windows Setup removed the compression flag from the partition it's installing to.
    In the end, I've come up with this:

    1) Copy "compact.exe" and "compact.exe.mui" from "%windir%\system32" and the respective language subdirectory "en-EN" - or whatever yours may be - onto the USB you're installing Windows from (Or include it on the disc, should you play it old-school ;) ), we'll need that later.
    2) Install Windows 7 as usual but boot from USB (or disc) after Setup reboots the second time.
    3) Open the Console in WinPE (Shift + F10 in case you didn't know) and identify the letter for the USB Flash (or disc) so you can start "compact.exe". This can be a bit tricky though. Results may vary due to different hardware setups (number of drives etc). When in doubt, use "dir /p" to see the descriptions of the drives/partitions. Good "naming" helps there ;)
    4) Use "compact.exe" on the partition windows is installed on.
    5) ?
    6) Profit.

    The only thing I can't get rid of is "chkdsk" constantly freezing somewhere along its routine.
    If you cancel it Windows starts just fine and the entire partition is compressed (minus the odd vital file here and there).

    Results on my fresh install:
    81.175 files in 14.243 directories.
    12,753,533,306 bytes of data saved in 7,905,520,446 Bytes.
    Ratio 1,6 to 1.
    Saved ~4.5 GB and I haven't even started downloading updates and stuff.

    Would be grateful for a hint on the chkdsk issue though...

    1. Interesting approach, although not sure how effective that is for the likes of WinSxS, was this for generally freeing space?

      Anyway, I would check the Event Log > Application to see maybe what triggered CHKDSK. You could try disabling CHKDSK for a partition using CHKNTFS, but that's probably not a good idea, unles you want to boot it first and try other things later. Or more importantly try to avoid causing a CHKDSK in the first place by maybe not compressing important system files like pagefiles.sys, hiberfil.sys, bootmgr (more for you to find).

      Hope it helps.

    2. Hello again.

      I just tried this to see if I can do it. More like a proof of concept. Turned out I could... at a cost.
      As I stated in my previous Post, you can cut down disk useage to roughly 66% that way, but your machine is slowed down horribly.
      My Laptop needed ~8 minutes to boot after the Windows Setup was fully completed.

      The important system files like pagefule, hiberfil etc are never compressed. As far as I know Windows seems to automatically disable compression on system-critical files (although I didn't check explicitly).

      In the end I'd say you *can* compress the entire partition, but it won't do you much good in the long run since the system will run like a sleeping pill.

    3. Thanks for letting us know of the continuation of your experiment.

      I don't know if those 8 minutes are just the first boot after Windows Setup - maybe it needes to recreate the pagefile, hibernate file etc uncompressed, or every boot takes longer - in which case I would use the Windows Performance Toolkit (WPT) to see what's spending the time on.

      I haven't mentioned but NTFS compressing files like any other file operation is not happening tightly in-place so it will most likely fragment it. In your case it might be too early to run defrag.exec, or maybe look at Sysinternals Contig.

      Because I use defragmeting tools like Piriform Defraggler on all my HDD machines I usually forget to recommend them thinking that everyone does know about them and uses one - I like it cause for one it can defragment the swapfile etc at boot time and second you can schedule it for family member's machines. So for anyone doing the whole NTFS compressing detailed in the blog post, do run a manual defrag when finished with Windows Defrag or similar.

      Last but not least, I generally do not compress entire partitions unless of course you really are running out of space, and only compress files that I know will be mostly used for reading - that is because writing frequently to compressed files is more costly and might not worth it. See this "Best practices for NTFS compression in Windows" article from Microsoft for a quick overview - http://support.microsoft.com/en-us/kb/251186

  37. Great article, just got back 6GB!

  38. You could use ZIPmagic DriveSpace to do this for you in a single click, with a nice GUI, and additionally, with multi-threading so the process completes 10-20 times faster as long as you have a decent SSD and a 4-8 core CPU.

  39. Dan, dittos on all the kudos. This is a great procedure that I have bookmarked for future use on servers that are low on C: space. A few of questions:

    1. I was able to disable TrustedInstaller, but for msiconfig, I got "ChangeServiceConfig FAILED 5: Access is denied." I started CMD as admin, of course, first having logged in as a domain admin, and then again logged in as the local server's admin. No success on that either way. Nonetheless, I was able to do the rest of the steps. (fingers crossed as to the success.)

    2. If the compress is successful, why the Restore?

    3. When I usually compress folders, the highest level -- WinSxS in this case -- displays as light blue, which I think means any future additions to the folder will also be compressed. After running your procedure, I see all subfolders compressed (blue) but not WinSxS. So I'm guessing future Windows updates will add stuff to WinSxS uncompressed.

    Again, many thanks and congrats on your obvious popularity.

    1. Thanks Doug, great comments.

      1. I don't know what happened there, but to be honest I don't think it will make much difference. The idea behind stopping / disabling those services would be to prevent them from keeping some of those files in use. The worst that would happen is that some would not be compressed (see /i paramter for compact).

      2. You want to restore the ACLs on the WinSXS subfolders or you'll break future installations. I'm surprised I haven't mentioned this before, I guess I was just happy I found a working solution in the end. See this post a few months before - http://dandar3.blogspot.com/2012/07/case-of-breaking-winsxs-folder-security.html

      3. That was a mistake on my part, you're perfectly right - I must have tried a few things and read the COMPACT switches wrong, the filename (*) is optional and specifying it will make it compress the contents but not mark the folder to compress future files. /S:"PATH" on its own seems to compress the folder and everything in it. I will update the post accordingly, thanks.