Author Posts

December 23, 2014 at 7:01 am

I have a csv file I am importing and creating a PSObject

I would like to take the 1st name's 1st letter and last name and create an email address.

Some names but not all have a middle initial which needs to be excluded from the email address

example

JAMES J. JONES
JILLIAN WARD
ANDREA SMITH
MATTHEW J. RODRIGUEZ

all emails will end with "@mycompany.com"

any help would be greatly appreciated

December 23, 2014 at 8:01 am

Is this from a text file, csv, or excel spreadsheet?

December 23, 2014 at 10:02 am

Assuming your script has a First, Middle, and Last column, (or just first and last actually) you can do this:

$data = Import-Csv 'C:\scripts\email.csv'

ForEach[$Name in $data]{
                        $FName = $Name.First
                        $First = $FName.substring[0,1]
                        $Last = $Name.Last
                        $Email = $First + $Last + '@company.com'
}

Then you can feed the email address into whatever script you need to dump the emails into.

December 23, 2014 at 10:07 am

Actually, you could do it this way too I suppose. But I think it looks cleaner if you define the last name as a variable too...

$data = Import-Csv 'C:\scripts\email.csv'

ForEach[$Name in $data]{
                        $FName = $Name.First
                        $First = $FName.substring[0,1]
                        $Email = $First + $Name.Last + '@company.com'
                        $Email
}

December 23, 2014 at 2:27 pm

Hey Will it's just one column name full name

December 23, 2014 at 5:49 pm

I'm starting to think you just enjoy making me think harder. 😉

$data = Import-Csv 'C:\scripts\names.csv' -Header Names
 
ForEach[$Name in $data]{
                        $FName = $Name.Names
                        $First = $FName.substring[0,1]
                        $Last = $FName.Split[" "] | Select-Object -Last 1
                        $Email = $First + $Last + '@company.com'
                        $Email

}

This will do what you're looking for. If your CSV has a header row, just remove the '-Header Names' part.

December 23, 2014 at 5:57 pm

Well Done Will!

Thank you 😉

December 23, 2014 at 6:11 pm

One more twist and this would just be a bonus

we have a dozen people with popular names example Lee jones, theres also a Lou Jones.

So to create an email account we needed to use The 1st 2 initails in the first name for some user's ..pretty common place.

Any way to add some logic an If statement or a -like | -match operator that would identify a list of providied 1st names , where if its that 1st name like " Lee" then use 1st 2 initials for email address.

Either way thank you for all your help 🙂

December 23, 2014 at 6:17 pm

You could, depending on what database you're looking at to verify the names. For instance, if you're using Active Directory. I 'll have to pull in my buddy Stephen for some help on this as he's a bit more of an AD guru than I. I'll ping him. 😛

December 23, 2014 at 6:25 pm

ok sweet or how about an array like $watchlist = "Lee Watson" , "Mike Jones" , "John Doe" ... just to simplify it

I'd really like to see both options if possible... Raising the Bar 😉 l

Thx Will

December 24, 2014 at 6:06 am

Hi gents!

Let's start with the excellent code we have from Will so far, it isn't too hard to check and see if a user exists with this name. If not, create mailbox, if so, try something different.

$data = Import-Csv 'C:\scripts\names.csv' -Header Names
 
ForEach[$Name in $data]{
                        $FName = $Name.Names
                        $First = $FName.substring[0,1]
                        $Last = $FName.Split[" "] | Select-Object -Last 1
                        $Email = $First + $Last + '@company.com'
                        if [[Get-ADUser $Email].Count -eq 0]{
                           #If no users exist with this e-mail addy
                           Enable-Mailbox $Email 
                        }
                        ELSE{
                           #If there is a user with this address [we need to try another name]
                           $First = $FName.substring[0,2]
                           $Last = $FName.Split[" "] | Select-Object -Last 1
                           $Email = $First + $Last + '@company.com'
                           Enable-Mailbox $Email
                        }
 
}

This logic isn't fully baked (for instance, what if you hired ten guys, named Steve Owen, Steven Owen, Stephen Owen, Stephanie Owen, etc!). If we wanted to procedurally catch for this, we can use this code below, allowing up to five letters in the user's name.

$data = Import-Csv 'C:\scripts\names.csv' -Header Names
 
ForEach[$Name in $data]{
    :countingLoop [1..5] | ForEach-Object { 
        $FName = $Name.Names
        $First = $FName.substring[0,$_]
        $Last = $FName.Split[" "] | Select-Object -Last 1
        $Email = $First + $Last + '@company.com'
        if [[Get-ADUser $Email].Count -eq 0]{
            #If no users exist with this e-mail addy, make one and exit
            Enable-Mailbox $Email 
            break countingloop
        }
    }
}

December 24, 2014 at 8:54 am

Hey Stephen

Thank you for the impressive solution you came up with.

I'm looking not so much to create/enable the email but more to retrieve the right email so i can use it with send-email cmdlet for a report I am doing . Thats why i need to be able to indicate a watch list of sorts to add the 2nd letter of the username

all the emails are enabled

December 24, 2014 at 12:01 pm

You finesse not knowing about a middle initial by selecting the last element in a collection.

$name = ["Robert F. McCoy"].ToLower[] -split " "
$emailAddr = "$[$name[-1]]$[$name[0].Substring[0,1]]@mycomany.com"
$emailAddr