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)


32 comments :

  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!

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

    ReplyDelete
  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 ;)

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

    ReplyDelete
  5. Thanks Pedro,

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

    ReplyDelete
  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!

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

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

    ReplyDelete
  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

    ReplyDelete
  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!

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

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

    ReplyDelete
  13. Dan Dar3

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

    Mike

    ReplyDelete
  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

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

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

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

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

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

    ReplyDelete
  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'?

    ReplyDelete
  21. Dan Dar 3.

    A big thanks for this.

    THANKS!!

    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!

    Kev.

    ReplyDelete
  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!!)

    Kev

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

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

    ReplyDelete
  24. @Dan Dar 3

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

    TIA Kev

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

    Thanks!

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

    Excellent post, thank you

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

    ReplyDelete
  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] }

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

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

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

    ReplyDelete