Monday, December 31, 2012

Create a Group and User using Powershell

Create a Group and User using Powershell

$cmpName = [System.Net.DNS]::GetHostName()
#Load the Computer WinNT(Windows New Technology) into an ADSI (Active Directory Service Interface) defined object.
$computer = [ADSI]("WinNT://$cmpName,computer")
#Join the computer to the Home network named WorkGroup
$computer.JoinDomainOrWorkgroup("WORKGROUP")

#List all the computers users and groups
$computer.Children | Where {$_.SchemaClassName -ne "service"} | Select *Name

#Create A Family Group
$newGroup = $computer.Create("group","Family")
$newGroup.psbase.InvokeSet("description","Family Group")
$newGroup.setinfo()

#Give the Family Group (which includes all members) to the Admin group
$adminGroup = "$cmpName/Administrators,group"
$adminGroup = [adsi]("WinNT://$adminGroup")
$adminGroup.add("WinNT://$cmpName/Family")

#Create a new user
$newuser = $computer.Create("user", "Test")
$newuser.SetPassword("Password1")
$newuser.SetInfo()
$newuser.psbase.InvokeSet('AccountDisabled', $false)
$newuser.psbase.InvokeSet('FullName','FirstName LastName')
$newuser.SetInfo()

#Add user to the Family group
$newGroup.psbase.Invoke("Add","WindNT://"+$newuser.Name)

#List all the groups and users on this computer
$computer.Children | Where {$_.SchemaClassName -eq "group" -or $_.SchemaClassName -eq "user"} | Select SchemaClassName, Name , Path

No comments:

Post a Comment