Avatar of Roccat
Roccat
Flag for United States of America

asked on 

Help improving AD user create script.

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

Powershell

Avatar of undefined
Last Comment
Roccat

8/22/2022 - Mon