Naimad
Naimad

Reputation: 39

Powershell get first name without second name

I have a script which writes in a Word-Document. Now i have to change the genders (i do this with a list of firstnames), the problem is a lot of people here have second names either with a "-" or with a space between.

Example

John-Mark smith
Alexander Robert Bails
Sasha baster

My Script only works if i can get the very first name (John/Alexander/Sasha), but i don't know how i can do this. I Get the users out of a txt. file with following titles: ID,firstname,lastname,Account,rndPW,SMTP,...,...

$smtp = $_.smtp
$username = $_.account
$name = $_.lastname
$name1 = $_.lastname
$name2 = $_.lastname
$fistname= $_.firstname
$firstname1= $_.firstname
$passwort = $_.rndPW


$strTitel = "Dear Sir/Madam "+$name
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_male.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Sir"+$name }
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_female.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Madam"+$name }
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_unspecific.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Sir / Madam"+$name }

Upvotes: 0

Views: 408

Answers (1)

sfphoton
sfphoton

Reputation: 177

I assume the $name variable is the name as a string. We just need its first part until the first space or dash (-) character.

$nameslist = $name.Replace("-", " ").Split(" ")
$firstfirstname = $nameslist | Select-Object -first 1

I guess the very first first name determines gender, no need to take all of them in consideration.

Upvotes: 2

Related Questions