how to add a domain user as a local administrator ( add to local admin group) using a vbs script

ishara perera 6 Reputation points

Hi I tried several combinations just need to prompt user to enter the domain user account that need to be added as a local administrator on the current PC and let them type the username and need to hardcode the domain admin account in the vbs script can we do this. I tried this but it did not work for me

Option Explicit

Dim domainAdmin, domainAdminPassword, domainUserName, group, groupMember
Dim objNetwork, objGroup, objUser

' Prompt user for domain user name
domainUserName = InputBox("Enter the domain user name to add to the local Administrators group:", "Add User to Local Administrators Group")

' Hard-code domain admin credentials
domainAdmin = ""
domainAdminPassword = ""

' Get a reference to the local Administrators group
Set objGroup = GetObject("WinNT://./Administrators")

' Get a reference to the user to add to the group
Set objUser = GetObject("WinNT://" & domainUserName)

' Add the user to the local Administrators group
On Error Resume Next
If Err.Number = 0 Then
    ' Display success message
    MsgBox "User " & domainUserName & " has been added to the local Administrators group.", vbInformation, "User Added"
    ' Display error message
    MsgBox "Failed to add user " & domainUserName & " to the local Administrators group. Error: " & Err.Description, vbCritical, "Error Adding User"
End If

' Clean up
Set objUser = Nothing
Set objGroup = Nothing

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,383 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,139 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points

    Since you've chosen use a "PowerShell" tag in your question, why not use PowerShell to accomplish this? The Add-LocalGroupMember cmdlet does it quite easily, as does the Read-Host cmdlet for prompting for and reading the account.

    $u = Read-Host "Enter the domain account (e.g., 'domain\user'): "
    Add-LocalGroupMember -Group Administrators -Member $u