r/PowerShell • • Dec 04 '25

Scripting Help

Hello everyone. I am trying to create a script that creates new ad users by using a csv file. I am struggling with the display name variable. I am setting display name has a variable. I have $DisplayName = “($user.’FIRST NAME’) + ($user.’LAST NAME’)”. Can someone help me figure out why it’s not working?

2 Upvotes

23 comments sorted by

View all comments

Show parent comment

4

u/BlackV Dec 04 '25

A more complex example

Create Remote mailbox

$UserSplat = @{
    Name                         = $SingleReport.DisplayName
    FirstName                    = $SingleReport.firstName
    LastName                     = $SingleReport.LastName
    SamAccountName               = $SingleReport.SAMName
    UserPrincipalName            = $SingleReport.UPN
    PrimarySmtpAddress           = $SingleReport.emailPref
    DomainController             = $ADController.HostName 
    Password                     = $SingleReport.Password
    RemotePowerShellEnabled      = $false
    ResetPasswordOnNextLogon     = $false
    OnPremisesOrganizationalUnit = $SingleReport.OU
}

$NewMailbox = New-RemoteMailbox @UserSplat -erroraction stop

Get the user that was created with New-RemoteMailbox

$NewAdUser = Get-ADUser -Identity $SingleReport.SAMName -ErrorAction Stop -Server $ADController.HostName

Set country cleanly

$ReplaceCountry = @{
    c           = $($SingleReport.CountryCodes.c)
    co          = $($SingleReport.CountryCodes.co)
    countrycode = $($SingleReport.CountryCodes.countrycode)
    }
$NewAdUser | Set-ADUser -Replace $ReplaceCountry -Server $ADController.HostName 

Set Dept details

$DeptSplat = @{
    Title       = $SingleReport.Position
    Description = $SingleReport.Position
    Manager     = $SingleReport.Manager
    EmployeeID  = $SingleReport.PersonNumber
    Enabled     = $false
    Server      = $ADController.HostName 
    }
$NewAdUser | Set-ADUser @DeptSplat

Essentially this is all fed from some dirty CSV somewhere, but I'm re using my objects where possible