Splitting a word

Welcome Forums General PowerShell Q&A Splitting a word

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
2 months ago.

  • Author
    Posts
  • #111701

    Participant
    Points: 0
    Rank: Member

    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"
  • #111704

    Participant
    Points: 135
    Helping Hand
    Rank: Participant

    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"
    
    $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"
    }
  • #111721

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    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
    
  • #111742
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant
    $user = ($ln[0..4] + $fn[0..1]) -join ''
    • #111791

      Participant
      Points: 135
      Helping Hand
      Rank: Participant
      $user = ($ln[0..4] + $fn[0..1]) -join ''

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

You must be logged in to reply to this topic.