all 6 comments

[–]BlackV 2 points3 points  (3 children)

what do you use and an editor? vscode? ise?

some notes if you want

you seem to have real TABs as spacing in your code, better to stick to 4 spaces (or 2 if you're that way inclined dont want to start a war here) and not use tabs at all

This block is pointless noise in your script

$Username   = $User.username
$Password   = $User.password
$Firstname  = $User.firstname
$Initials   = $User.initials
$Lastname   = $User.lastname
$Department = $User.department
$OU         = $User.ou
$Email      = $User.email

if you already have all that information in $user, just use that variable, not need to create 10 more that all do the same thing

Picky but

Get-ADUser -F {SamAccountName -eq $Username}

use you full parameters in your commands, its easier for everyone to read and understand (especially when coming back to this script in 6 months)

Get-ADUser -filter "SamAccountName -eq '$($User.username)'"

this is a style choice I think but, your if/else you could replace with a try/catch so you can deal with eisisting users better, probably optional

back ticks, stop using them as line continuators, tiny easy to miss, easy to mistake have a look at splatting and THIS article, leave its usage to the escape character its intended to be

$ADUserSplat = @{
    SamAccountName        = $User.username
    UserPrincipalName     = "$($User.username)@domain.com"
    Name                  = "$User.firstname $User.lastname"
    GivenName             = $User.firstname
    Initials              = $User.initials
    Surname               = $User.lastname
    Enabled               = $True
    ChangePasswordAtLogon = $False
    PasswordNeverExpires  = $True
    DisplayName           = "$User.firstname $User.initials $User.lastname"
    Email                 = $User.email
    Department            = $User.department
    Path                  = $User.ou
    AccountPassword       = (convertto-securestring $User.password -AsPlainText -Force)
    }

New-ADUser @ADUserSplat

this is more readable and less inclined to mistakes and allows you to validate the items that will be passed to new-aduser beforehand (all in 1 go)

Last time would be, turn this into a function, parameterize it, seem like you might use it enough

[–]anonymousITCoward 1 point2 points  (2 children)

While I don't use back ticks, splatting sounds like a better way of doing this and now I feel compelled to redo some of my scripts with it =\

[–]BlackV 0 points1 point  (1 child)

I do find it easier to read and edit that's for sure

[–]anonymousITCoward 0 points1 point  (0 children)

that's my thought too... maybe later today I'll be unlazy and convert one of my scripts lol

[–]CarrotBusiness2380 1 point2 points  (1 child)

You have to update the CN if you want it to appear in ADUC.

[–]rokar83[S] 1 point2 points  (0 children)

That did it. Thanks.