Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell for AD account import - need string help

Posted on 2013-12-10
3
Medium Priority
?
164 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 41

Accepted Solution

by:
footech earned 2000 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

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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 …
Suggested Courses

916 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