Solved

Powershell for AD account import - need string help

Posted on 2013-12-10
3
159 Views
Last Modified: 2014-10-06
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!
0
Comment
Question by:Diesel79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39709000
Try $GivenName.substring(0,3)
The first number is the starting position, the second is how many characters to include.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question