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(“@”).Replace(‘.’,’ ‘)
$Firstname = $User
$LastName = $User
$FirstInital = $FirstName.substring(0,1)
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.