Author Posts

September 13, 2018 at 10:04 am


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\$($\$($user.SamAccountName)"
$User | Add-Member –MemberType NoteProperty -Name HomeDrive -Value "L:"
$user | Add-Member –MemberType NoteProperty -Name UserPrincipalName -Value "$($user.SamAccountName)@$"

September 13, 2018 at 10:11 am

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

$fn = read-host -Prompt "Firstname"
$ln = read-host -Prompt "Lastname"


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\$($\$($user.SamAccountName)"
    HomeDrive             = "L:"
    UserPrincipalName     = "$($user.SamAccountName)@$"

September 13, 2018 at 12:54 pm

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

September 13, 2018 at 5:25 pm

$user = ($ln[0..4] + $fn[0..1]) -join ''

September 14, 2018 at 9:26 am

$user = ($ln[0..4] + $fn[0..1]) -join ''

Neat .... and there's no problem with shorter names. Cool. 😉