Skip to content

Instantly share code, notes, and snippets.

@kurtisdunn
Created May 28, 2019 23:05
Show Gist options
  • Save kurtisdunn/ef7492c56ba6882415d30f92dc413096 to your computer and use it in GitHub Desktop.
Save kurtisdunn/ef7492c56ba6882415d30f92dc413096 to your computer and use it in GitHub Desktop.
Powershell AD New User
# New user script


#write title
Write-Host "New User Creator 1.0"

#import the active directory module
Import-Module ActiveDirectory



#gathering the data


#Ask for first name
$firstname = Read-Host "First Name?"

#Ask for last name
$lastname = Read-Host "Last name?"

#Username
$username = "$firstname.$lastname"

#ask fo the password
$password = Read-Host "Password?

#ask for the office, this uses multiple choice switching
Write-Host "Select PHN Office"
Write-Host "1 - Bathurst"
Write-Host "2 - Bourke"
Write-Host "3 - Broken Hill"
Write-Host "4 - Dubbo"
Write-Host "5 - Orange"

#creates a variable for the office
$office = Read-Host 'office'

switch($office){
    1{ $office = 'template 1@test.local'}
    2{ $office = 'template 2@test.local'}
    3{ $office = 'template 3@test.local'}
    default{Write-Warning 'invalid input'}
}


$office

#within this parameter is where the actual user is created

    [Parameter(Mandatory=$True)]
    $firstname,
    [Parameter(Mandatory=$True)]
    $lastname,
    
    [Parameter(Mandatory=$True)]    
    $office,
    $password,
    [switch]$enabled,
    $changepw = $false,
    $ou,
    [switch]$useTemplateOU
    


$name ="$firstname $lastname"
#Username
$username = "$firstname.$lastname"
$samaccountname = "$username"
$passwprd_ss = ConvertTo-SecureString -String $password -AsPlainText -Force
$template_obj = Get-ADUser -Identity $office

If ($useTemplateOU) {
    $ou = $template_obj.DistinguishedName -replace '^cn=.+?(?<!\\),'
}
$params = @{
    "Instance"=$template_obj
    "Name"=$name
    "DisplayName"=$name
    "GivenName"=$firstname
    "SurName"=$lastname
    "AccountPassword"=$password_ss
    "Enabled"=$enabled
    "ChangePasswordAtLogon"=$changepw
}

If ($ou) {
    $params.Add("Path",$ou)
}
New-ADUser @params   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment