Splitting a word

Hi,

I’m trying to build an script where I need 2 variables to come together and make a new variable (word). Our users in our AD are based on a policy. The firstname and lastname. we take the first 5 characters of the lastname and the first 2 characters from their firstname.

My fullname is Damian Eickhoff, my username is Eickhda. Now I created a small input form where I can enter the Firtsname and Lastname as 2 individual variables but I can’t find out how I can glue those together based on our policy.

$fn = read-host -Prompt "Firstname"
$ln = read-host -Prompt "Lastname"
$org = read-host -Prompt "Organization"
$orgc = read-host -Prompt "Organization code"

$user = $fn.split[-5]


$User | Add-Member –MemberType NoteProperty -Name ChangePasswordAtLogon -Value $true
$User | Add-Member –MemberType NoteProperty -Name HomeDirectory -Value "\\Server\Home\$($orgc.cc)\$($user.SamAccountName)"
$User | Add-Member –MemberType NoteProperty -Name HomeDrive -Value "L:"
$user | Add-Member –MemberType NoteProperty -Name UserPrincipalName -Value "$($user.SamAccountName)@$org.nl"

Actually really easy but you should make sure the names are long enough!! :wink: … or you have to deal with shorter names in a defined way.

$fn = read-host -Prompt “Firstname”
$ln = read-host -Prompt “Lastname”

$fn.Substring(0,2)
$ln.Substring(0,5)

AND … there’s an easier way to create an object to store your user attributes …

$fn = read-host -Prompt “Firstname”
$ln = read-host -Prompt “Lastname”
$org = read-host -Prompt “Organization”
$orgc = read-host -Prompt “Organization code”

$User = [PSCustomObject]@{
Name = $ln.Substring(0, 5) + $fn.Substring(0, 2)
ChangePasswordAtLogon = $true
HomeDirectory = “\Server\Home$($orgc.cc)$(user.SamAccountName)" HomeDrive = "L:" UserPrincipalName = "($user.SamAccountName)@$org.nl”
}

There are multiple ways to do string concatenation:

$firstName = 'Damian'
$lastName = 'Eickhoff'

"$firstName $lastName" #Variables in double qoutes are expanded
$firstName + ' ' + $lastName #Traditional string concantenation
'{0} {1}. How are you today, {0}?' -f $firstName, $lastName #String Format, using array placeholders
$user = ($ln[0..4] + $fn[0..1]) -join ''
$user = ($ln[0..4] + $fn[0..1]) -join ''
Neat .... and there's no problem with shorter names. Cool. ;-)