Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ


Function to check if a user is an administrator on the local machine by djhawthorn
Posted: 7 Feb 05 (Edited 24 Jun 12)

Two versions; one reliant on WMI and the other on registry write/cleanup


'* User Is Administrator (Function)
'* Version 4.0
'This function checks to see if the currently logged-in user is an administrator
'Note: Requires WMI
Function UserIsAdministrator
'The local machine
strComputer = "."

'Connect to the registry using WMI
Set fncObjReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

'The key path to check access to
fncStrKeyPath = "SYSTEM\CurrentControlSet"

'Check the users access to delete this component of the registry, and return the result
fncObjReg.CheckAccess HKEY_LOCAL_MACHINE, fncStrKeyPath, DELETE, UserIsAdministrator
End Function

Set WshShell = CreateObject("Wscript.Shell")

'* User Is Administrator (Function)
'* Version 3.01
'This function checks to see if the currently logged-in user is an administrator
Function UserIsAdministrator
'If we cant create the registry entry when we try, it will return an error, so we need to resume
On Error Resume Next

'Try to write to the root of the registry (only Administrator's can do this)
WshShell.RegWrite "HKLM\TestAdminAccess", "Access Granted"

'Catch the error
If (Err.Number <> 0) Then
'If it errors, we can't create the reg key
UserIsAdministrator = false
'Otherwise, we are an administrator; remove the key
WshShell.RegDelete "HKLM\TestAdminAccess"

'Set the flag to say the user has access
UserIsAdministrator = true
End If

'Clear any errors

'Turn error reporting back on
On Error Goto 0
End Function

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close