Link to home
Start Free TrialLog in
Avatar of Diesel79
Diesel79

asked on

Powershell for AD account import - need string help

I have a powershell script that I found out on the internet that does a good job of AD account imports but with the amount of accounts I have its current setup will not work for me. I need to include the first three letters of the first name along with the last name for the samaccount to be successful. I have too many duplicate accounts otherwise.  Right now "John Doe" comes out as "DoeJ" but I would like the result to be "DoeJoh"

My current function is this:
Function Set-sAMAccountName {
    Param([Switch]$Csv=$false)
    if(!$Csv)
        {
        $GivenName = $txtFirstName.text
        $SurName = $txtLastName.text
        }
    else{}
    Switch($XML.Options.Settings.sAMAccountName.Style | Where{$_.Enabled -eq $True} | Select -ExpandProperty Format)
        {
        "FirstName.LastName"    {"{0}.{1}" -f $GivenName,$Surname}
        "FirstInitialLastName"  {"{0}{1}" -f ($GivenName)[0],$SurName}
        "LastNameFirstInitial"  {"{0}{1}" -f $SurName,($GivenName)[0]}
        Default                 {"{0}.{1}" -f $GivenName,$Surname}
        }
    }



But if I try to change the below line to include a start and end character the script dies.
  "FirstInitialLastName"  {"{0}{1}" -f ($GivenName)[0,3],$SurName}


Any help would be great!
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial