Getting environment variables from registry

This script comes from ASPN (posted from Denis Barmenkov) and prints the environment variables got from the Windows registry (\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment).

# -*- coding: Windows-1251 -*-

Get SYSTEM environment value, as if running under Service or SYSTEM account

Author: Denis Barmenkov 

Copyright: this code is free, but if you want to use it, 
           please keep this multiline comment along with function source. 
           Thank you.

2006-01-28 15:30

import os, win32api, win32con

def getenv_system(varname, default=''):
    v = default
        rkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
            v = str(win32api.RegQueryValueEx(rkey, varname)[0])
            v = win32api.ExpandEnvironmentStrings(v)
    return v

print 'SYSTEM.TEMP => %s' % getenv_system('TEMP')
print 'USER.TEMP   => %s' % os.getenv('TEMP')
print 'USER.PATH   => %s' % os.getenv('PATH')

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.