Sub UAC() 'Run this subroutine FIRST THING on any script that may require admin 'priveleges. It will generate a "User Access Control" prompt on Vista 'or generate a "Run As" prompt for admin priveleges on 2K/XP (if the '2K/XP user isn't an admin). It will do nothing on Win9x systems or 'on Win2K/XP systems with a logged-in admin. Const FOR_WRITING = 2 Const TEMP_FOLDER = 2 Dim ws, fs, ts, wmi, col, obj Dim strData, strUacFile, strArg, strArgs, strOsVersion, strUserName, strGroup, strMember Dim lngArg, lngOsVersion Dim blnIsAdmin, blnHasAdmins 'See if we can create needed objects On Error Resume Next Err.Clear Set fs = CreateObject("Scripting.FileSystemObject") Set ws = CreateObject("WScript.Shell") Set wmi = GetObject("winmgmts:\\.\root\CIMV2") If Err.Number <> 0 Then Exit Sub 'Reasonable assumption it's Win9x? On Error Goto 0 'Define the name of the special script that will re-launch this one for UAC if needed. 'Can't use %TEMP% or other per-user folder, can't use script folder because it might be 'in protected area. strUacFile = "" If strUacFile = "" Then 'First try to use "shared docs" because everyone can get to it. strUacFile = ws.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common Documents") If strUacFile <> "" Then If fs.FolderExists(strUacFile) Then strUacFile = fs.BuildPath(strUacFile, "~" & fs.GetBaseName(WScript.ScriptName) & ".vbs") Else strUacFile = "" End If End If End If If strUacFile = "" Then 'Last choice is the drive root. At least we know it exists! strUacFile = fs.BuildPath(fs.GetDriveName(WScript.Path) & "\", "~" & fs.GetBaseName(WScript.ScriptName) & ".vbs") End If 'If the UAC script exists, we can assume it launched this one! It should be deleted. If fs.FileExists(strUacFile) Then fs.DeleteFile strUacFile Exit Sub End If 'Find the Operating System major version Set col = wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", 48) strOsVersion = "0.0" For Each obj In col : strOsVersion = obj.Version : Next If Instr(strOsVersion, ".") Then strOsVersion = Left(strOsVersion, Instr(strOsVersion, ".") - 1) lngOsVersion = CLng(strOsVersion) 'If the OS is less than W2K, everybody is an admin and no UAC prompt is needed If lngOsVersion < 5 Then Exit Sub 'Find the user name (needed to see if the user is an admin) Set col = wmi.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 48) strUserName = "" For Each obj In col : strUserName = obj.UserName : Next If Instr(strUserName, "\") Then strUserName = Mid(strUserName, Instr(strUserName, "\") + 1) 'See if the user is an admin blnIsAdmin = False blnHasAdmins = False Set col = wmi.ExecQuery("SELECT * FROM Win32_GroupUser", "WQL", 48) For Each obj In col strGroup = obj.GroupComponent strGroup = Split(strGroup, "=") If strGroup(UBound(strGroup)) = """Administrators""" Then blnHasAdmins = True strMember = obj.PartComponent strMember = Split(strMember, "=") If strMember(UBound(strMember)) = """" & strUserName & """" Then blnIsAdmin = True End If End If Next If blnHasAdmins = False Then blnIsAdmin = True 'If no admin group then everybody is an admin? 'Final test. No UAC prompt is needed if user is admin on something less than Vista If ((lngOsVersion < 6) And (blnIsAdmin)) Then Exit Sub 'This is definitely a directly-run script. We need to re-launch it to get a UAC. 'First collect any arguments the script has so we can re-launch it exactly the same. For lngArg = 0 To WScript.Arguments.Count - 1 If strArgs <> "" Then strArgs = strArgs & " " strArg = WScript.Arguments(lngArg) If ((InStr(strArg, " ") <> 0) Or (InStr(strArg, vbTab) <> 0)) Then strArg = """" & """" & strArg & """" & """" End If strArgs = strArgs & strArg Next 'Now build the actual command that will re-launch the script with a UAC prompt strData = "CreateObject(""Shell.Application"").ShellExecute " strData = strData & """" & """" & """" & Wscript.FullName & """" & """" & """" strData = strData & ", " strData = strData & """" & """" & """" & WScript.ScriptFullName & """" & """" If strArgs = "" Then strData = strData & """, " Else strData = strData & " " & strArgs & """, " End If strData = strData & """" & """" & """" & fs.GetParentFolderName(WScript.ScriptFullName) & """" & """" & """" strData = strData & ", " strData = strData & """runas""" strData = strData & ", 1" 'Save the UAC command in a separate script Set ts = fs.OpenTextFile(strUacFile, FOR_WRITING, True) ts.Write strData ts.Close 'Show a message Wscript.Echo "This script will need administrative priveleges." 'Launch the UAC script ws.Run "wscript.exe" & " """ & strUacFile & """", 1, False 'We MUST exit at this point and let the UAC script re-launch us. WScript.Quit End Sub