Using Python to Reduce the Roaming Profile

Roaming Profiles are a blessing and a curse. If the user uses the internet, their browser’s cached files will grow like mad. If the user downloads programs to their desktop or creates large Powerpoint files anywhere in their profile, then they have to be managed whenever the user logs in or out. There are several solutions to this problem: disk quotas, blocking the ability to download or put stuff in one’s profile, etc. In this article, I will show you how to exclude specific directories from the user’s profile using Python.

This is basically just a Windows Registry hack. As always, be sure to back up your Registry before applying any changes to it in case something goes horribly awry and you make your machine unbootable.

from _winreg import *

    key = OpenKey(HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",
                  0, KEY_ALL_ACCESS)
except WindowsError:
    key = CreateKey(HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")

# Exclude directories from roaming profile 
prof_dirs = "Local Settings;Temporary Internet Files;History;Temp;My Documents;Recent"
SetValueEx(key, "ExcludeProfileDirs", 0, REG_SZ, prof_dirs)     

This code is pretty simple. First we imported the various modules and constants from _winreg. Then we tried to open the appropriate Registry key and created it if the key didn’t already exist. Next we created a string of semi-colon delimited directories to exclude from the roaming profile. Finally, we set the appropriate value and closed the key.

And that’s all there is to this simple script!