Solved

Powershell for AD account import - need string help

Posted on 2013-12-10
3
153 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 39

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now