Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to use powershell to add a spesific user to the local administrator group on a machine. I would be running the powershell script in the context of a user that has Administration rights on the local machine.

Thanks Rihan

share|improve this question
    
See also [Windows Local Account and Group Maintenance][1]. [1]: serverfault.com/questions/31058/… –  Nathan Hartley Feb 24 '11 at 17:19

3 Answers 3

up vote 4 down vote accepted

This is the Advanced Function That I use to add a users to the local Administrator group using Powershell on several computers.

Usage: Get-Content C:\Computers.txt | Set-LocalAdminGroupMembership -Account 'YourAccount'


Function Global:Set-LocalAdminGroupMembership
{


    <#
    .Synopsis

    .Description

    .Parameter $ComputerName,

    .Example
     PS> Set-LocalAdminGroupMembership -ComputerName $ComputerName -Account 'YourAccount'

    .Link
     about_functions
     about_functions_advanced
     about_functions_advanced_methods
     about_functions_advanced_parameters

    .Notes
     NAME:      Set-LocalAdminGroupMembership
     AUTHOR:    Innotask.com\dmiller
     LASTEDIT:  2/4/2010 2:30:05 PM
     #Requires -Version 2.0
    #>



    [CmdletBinding()]
    param(
    [Parameter(Position=0, ValueFromPipeline=$true)]
    $ComputerName = '.',
    [Parameter(Position=1, Mandatory=$true)]
    $Account
    )


    Process
    {  

        if($ComputerName -eq '.'){$ComputerName = (get-WmiObject win32_computersystem).Name}    
        $ComputerName = $ComputerName.ToUpper()


        $Domain = $env:USERDNSDOMAIN

        if($Domain){
            $adsi = [ADSI]"WinNT://$ComputerName/administrators,group"
            $adsi.add("WinNT://$Domain/$Account,group")
            }else{
            Write-Host "Not connected to a domain." -foregroundcolor "red"
            }


    }# Process


}# Set-LocalAdminGroupMembership
share|improve this answer

Here is a simple 2 line script that performs this function

$group = [ADSI]("WinNT://"+$env:COMPUTERNAME+"/administrators,group")
$group.add("WinNT://$env:USERDOMAIN/usernameiwantoadd,user")

For more information see Hey, Scripting Guy! How Can I Use Windows PowerShell to Add a Domain User to a Local Group?

So there are a couple of notes. In the first line I used string concatonation, I didn't have to (see the next line) but i like to because it helps accentuate the variables I am using. Second, these lines will add a domain user ,if you wanted to add a local user just remove $env:USERDOMAIN/

share|improve this answer
    
I would like to run this script on workstations in such a way that the person running the script can type in a username. Hence the Read-Host –  Rihan Meij Mar 9 '10 at 8:09
    
so in that case make the first line $userInput = Read-Host "Enter username to add to the local admin group" then use $userinput where is says usernameiwantoadd –  Jim B Mar 9 '10 at 14:27

Here is another way to do this. Needs to be run in Administrator context:

$domain=""
$computername= "$env:computername"
$group=$computer.psbase.children.find("administrators")

function AddToGroup($number)
{
     $group.add("WinNT://"+$domain+"/"+$number )
}

#Add these domain users/groups to the local administrator group
 AddToGroup ""
 AddToGroup ""

#Add these domain computer accounts to the local administrator group. 
#Computer accounts always end with $.
AddToGroup "$"

More info here: http://www.danielclasson.com/powershell-script-to-add-domain-computers-groups-or-user-accounts-to-the-local-administrator-group/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.