Solved

Help improving AD user create script.

Posted on 2016-08-03
2
41 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 84

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

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 …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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