Solved

Help improving AD user create script.

Posted on 2016-08-03
2
35 Views
Last Modified: 2016-08-04
I am working on a script to create an active directory user and home folder. I am looking for some tips and perhaps some help changing the input so that I can put in a username in the format of LastName,FirstName. Then it would fill in the Name = First initial Lastn Name, SamAccountName = First initial LastName, GivenName = first name, Surname = last name, DisplayName = First Name Last Name , UserPrincipalName = first name last initial

import-module activedirectory
$UserN = Read-Host -Prompt 'Input the user name'
New-ADUser -name $UserN -SamAccountName $UserN -GivenName "FirstName" -Surname "LastName" -DisplayName "Whole Name"  -UserPrincipalName (“{0}@{1}” -f $UserN,”homelab.com”) -Path 'OU=Staff,DC=homelab,DC=com' -AccountPassword (ConvertTo-SecureString -AsPlainText "P@ssw0rd" -Force) -ChangePasswordAtLogon $true -Department "HTMB" -Description "Test Description" -EmailAddress "Test@homelab.com" -EmployeeID "123456" -Enabled $true -HomeDirectory "\\127.0.0.1\Share\$UserN" -HomeDrive 'h:'
$Path = "\\127.0.0.1\share\"

New-Item -type directory -path $path$UserN
$Acl = Get-Acl $path$userN
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule("$UserN","FullControl","Allow")
$Acl.SetAccessRule($Ar)
Set-Acl "$path$userN" $Acl

Open in new window

0
Comment
Question by:Roccat
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41742280
With a command like New-ADUser, which requires a lot of arguments, it's best to use a splatted hash table to provide the arguments.
$Input = Read-Host -Prompt "Input the user name as 'LastName, FirstName'"
If ($Input -notmatch '\A\s*(?<LastName>\w+)\s*,\s*(?<FirstName>\w+)\s*\Z') {
	"Invalid input '$($Input)'" | Write-Error
	Exit 1
}
$LastName = $Matches['LastName']
$FirstName = $Matches['FirstName']

$ADUser = [ordered]@{}
$ADUser['Name'] =					"$($FirstName[0])$($LastName)"
$ADUser['SamAccountName'] =			"$($FirstName[0])$($LastName)"
$ADUser['GivenName'] =				$FirstName
$ADUser['Surname'] =				$LastName
$ADUser['DisplayName'] =			"$($FirstName) $($LastName)"
$ADUser['UserPrincipalName'] =		"$($FirstName[0])$($LastName)@homelab.com"
$ADUser['Path'] =					'OU=Staff,DC=homelab,DC=com'
$ADUser['AccountPassword'] =		ConvertTo-SecureString -AsPlainText 'P@ssw0rd' -Force
$ADUser['ChangePasswordAtLogon'] =	$True
$ADUser['Department'] =				'HTMB'
$ADUser['Description'] =			'Test Description'
$ADUser['EmailAddress'] =			"$($FirstName[0])$($LastName)@homelab.com"
$ADUser['EmployeeID'] =				'123456'
$ADUser['Enabled'] =				$True
$ADUser['HomeDirectory'] =			"\\127.0.0.1\Share\$($ADUser['SamAccountName'])"
$ADUser['HomeDrive'] =				'H:'

"Creating a new user with the following properties:"
$ADUser.GetEnumerator() | Format-Table

New-ADUser @ADUser -WhatIf

Open in new window

0
 

Author Closing Comment

by:Roccat
ID: 41742816
This is great, Thank you!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question