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)