Solved

Powershell for AD account import - need string help

Posted on 2013-12-10
3
156 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
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

828 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