Last active
August 29, 2015 14:01
-
-
Save garystafford/8536fd958c3276fe612f to your computer and use it in GitHub Desktop.
Create a new local User with basic options and assign to new group. Used on Windows 2012 Server. Just change variables in head of script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################### | |
# Author: Gary A. Stafford | |
# Revised: 2014-05-29 | |
# Create new local User and Group | |
############################################################### | |
############################################################### | |
# Main variables (Change these!) | |
[string]$machineName = "MACHINE_NAME_HERE" # WIN-WEB018 | |
[string]$userName = "USER_NAME_HERE" # mjones | |
[string]$fullName = "FULL USER NAME HERE" # Mike Jones | |
[string]$password = "USER_PASSWORD_HERE" # pa$$w0RD! | |
[string]$groupName = "GROUP_NAME_HERE" # Development | |
############################################################### | |
# Create new local user account | |
[ADSI]$server = "WinNT://$machineName" | |
$newUser = $server.Create("User", $userName) | |
$newUser.SetPassword($password) | |
# $newUser | Get-Member # List members like below | |
$newUser.Put("FullName", "$fullName") | |
$newUser.Put("Description", "$fullName User Account") | |
# Assign flags to user | |
# flags: http://msdn.microsoft.com/en-us/library/aa772300%28v=vs.85%29.aspx | |
[int]$ADS_UF_PASSWD_CANT_CHANGE = 64 | |
[int]$ADS_UF_DONT_EXPIRE_PASSWD = 65536 | |
[int]$COMBINED_FLAG_VALUE = 65600 | |
[int]$flags = $newUser.UserFlags.value -bor $COMBINED_FLAG_VALUE | |
$newUser.put("userFlags", $flags) | |
$newUser.SetInfo() | |
Write-Host "*** New User '$userName' created." | |
# Create new local group | |
$newGroup = $server.Create("Group", $groupName) | |
$newGroup.Put("Description","$groupName Group") | |
$newGroup.SetInfo() | |
Write-Host "*** New Group '$groupName' created." | |
# Assign user to group | |
[string]$serverPath = $server.Path | |
$group = [ADSI]"$serverPath/$groupName, group" | |
$group.Add("$serverPath/$userName, user") | |
Write-Host "*** User '$userName' assigned to Group '$groupName'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment