Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Powerbits #3 – Adding a User to a Local Group

I’ve been negligent in this series. Maybe that means I’m just not looking things up that much. Well, no matter. Today I had to look this up so I thought I would post it.

Here’s how you add a user to a local group using Powershell:

$domain = "powertoe"
$username = "Toe"
$computername = "toepc"
$groupname = "Administrator" #of course

$objUser = [ADSI]("WinNT://$domain/$username")
$objGroup = [ADSI]("WinNT://$computername/$groupname")
$objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)

The funny thing is that the reason the script I had to look this up was to write a script that gave me one of those Powershell-is-so-damned-cool moments. I needed to set a user as admin on every computer connected to a vcenter instance:

$computers = Get-VM |%{$_.Name}
$script = {
    $objUser = [ADSI]("WinNT://Powertoe/toe")
    $objGroup = [ADSI]("WinNT://$($env:COMPUTERNAME)/Administrators")
    $objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)
}
Invoke-Command -ScriptBlock $script -ComputerName ($computers)

Did I just do that in a matter of seconds!!!?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: