Search code examples
vbscriptprivileges

Best way to know if a user has administrative privileges from a VBScript


I need to check whether the user executing the script has administrative privileges on the machine.

I have specified the user executing the script because the script could have been executed with a user other than the logged on using something similar to "Runas".

@Javier: Both solutions work in a PC with an English version of Windows installed but not if the installed is in different language. This is because the Administrators group doesn't exist, the name is different for instance in Spanish. I need the solution to work in all configurations.


Solution

  • You can use script if you want to see if the logged on user is an administrator

    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = objNetwork.ComputerName
    strUser = objNetwork.UserName
    
    isAdministrator = false
    
    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
    For Each objUser in objGroup.Members
        If objUser.Name = strUser Then
            isAdministrator = true        
        End If
    Next
    
    If isAdministrator Then
        Wscript.Echo strUser & " is a local administrator."
    Else
        Wscript.Echo strUser & " is not a local administrator."
    End If
    

    I am not sure how to handle it when the script is run with "Runas" I am afraid.