Trying to generate SamAccountName

I am trying to generate an Alias with the format of FirstInitialLastName for a user from an email address. For example, from “Test User” I want to generate “TUser”.

So far I have the code below and both $Email and $User work perfectly fine. But when I try to build the Alias it breaks. The $Alias variable ends up being the second character from the first name. And when the script runs I receive this error: “Method invocation failed because [System.Char] doesn’t contain a method named ‘Substring’.”

$Email = $_.’Email’

$User = $Email.split(“@”)[0].Replace(‘.’,’ ‘)

$Firstname = $User[0]

$LastName = $User[1]

$FirstInital = $FirstName.substring(0,1)

$Alias= “$FirstInital$LastName”

New-Mailcontact -Name $User -Alias $Alias -ExternalEmailAddress $_.’Email’ -OrganizationalUnit $OU

I’m not sure what I’m doing wrong but I’m hoping someone can point it out.

submitted by /u/Baron164
[link] [comments]

Leave a Reply