There is the task of sending a letter to a new domain user. The used variable $Name contains "Last Name, First Name, Middle Name". In the letter, you need to leave only the first name and middle name and remove the last name. How can I do that?

 Get-Mailbox | where {$_.WhenCreated -gt (get-date).adddays(-1)} | foreach{ $name = $_.Name $Body = «$name, мы рады приветствовать Вас в нашей компании" } 

We get: "Ivanov Ivan Ivanovich, we welcome you to our company"

    2 answers 2

    I am still the cyclist. Try dividing the $ name variable into an array.

     $namearr = $name.split() 

    And then, from the given array, select the sequence of the full name that interests you.

     $nameIO = $namearr[1] + " " + $namearr[2] 

    Respectively:

     $Body = «$nameIO, мы рады приветствовать Вас в нашей компании" 
    • Thank you very much!! It is this result and sought! - Alex Privatov

    Regex same:

     $name = "Фамилия Имя Отчество" # разбиваем по пробелам, берём вторую и третью части $NameIO = $name -replace '^(.+)\s(.+)\s(.+)$','$2 $3' 

    If you do not like Regex, then you can:

     $name = "Фамилия Имя Отчество" # разбиваем по пробелам, берём вторую и третью части массива (нумерация с нуля), # объединяем элементы массива в строку с разделителем ' ' (пробел) $NameIO = $name.Split()[1,2] -join ' '