Solved

Help improving AD user create script.

Posted on 2016-08-03
2
31 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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
This is great, Thank you!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now