John Adams
John Adams

Reputation: 31

How can I concatenate first and last name in powershell

FirstName,LastName,passcode,Title,Manager_Email
zack,Ased,breast<achievement?&childhood059,manager,[email protected]

How can I make the Name zack Ased and the username zack.Ased , here is the code I tried

Import-Module ActiveDirectory
$ADUsers = Import-Csv C:\users.csv
foreach ($User in $ADUsers) {
    $FirstName = $User.FirstName
    $LastName = $User.LastName
    $Name = $FirstName + " " + $LastName
    $username =  $FirstName.$LastName
    $password = $User.passcode
    $Title = $User.Title
    $Manager_Email = $User.Manager_Email
    New-ADUser -Name $Name -SamAccountName $username -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -Title $Title -ChangePasswordAtLogon $true
    Enable-AdAccount -Identity $username
    }

Upvotes: 0

Views: 177

Answers (1)

John Smith 82
John Smith 82

Reputation: 31

using -join:

$FirstName = 'John'
$LastName = 'Smith'

$Name = $FirstName, $LastName -join " " 
#the " " is the delimiter, you can input whatever you like, for example a dot:
$Name = $FirstName, $LastName -join "." 

see more examples on https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_join

Another method is by putting it in a new string:

$Name = "$($FirstName).$($LastName)"

There are several other possible methods of doing this.

Upvotes: 1

Related Questions