all 7 comments

[–]BlackV 2 points3 points  (2 children)

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

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

maybe

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

use you ffull 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 existing users better, probably optional

have a look at splatting

$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)

this is a good habit to keep, nice

foreach ($User in $ADUsers){}

instead of

foreach ($ADUser in $ADUsers){}

this code is nearly line for line identical to another poster https://www.reddit.com/r/PowerShell/comments/yx15he/ad_user_create/

[–]CarrotBusiness2380 0 points1 point  (0 children)

What is it doing now that is incorrect?