none
Retrive values left to @ with Powershell RRS feed

  • Question

  • Hi all

    I want to get only the value left to "@" of a user´s email address with Powershell. For example, if mail address is User1@domain.com, I would like to retrieve only User1.

    How can I achieve this with Powershell? Wich command or function must I use?

    Thanks!


    • Edited by SKM12 Friday, May 18, 2012 9:58 AM
    Friday, May 18, 2012 9:52 AM

Answers

  • Hello SKM12,

    You can follow these to do that:

    $Address=((Get-mailbox username).Primarysmtpaddress).toString()

    $Name=$Address.Split("@")[0]

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support

    • Marked as answer by SKM12 Monday, May 21, 2012 10:56 AM
    Monday, May 21, 2012 9:39 AM

All replies

  • Dear Fellow,

    An easy way out is that you can export the list of all your email addresses using powershell command Get-Mailbox | ft PrimarySmtpAddress | Out-File -Filepath "C:\emailAddresses.txt" and import this file in MS Excel and replace "@emaildomain.com" with blank.

    But why you want to do so? Are your email Alias different from user name or account logon names?

    If not, then you can simply export the list by using command get-mailbox | ft alias | Out-File -Filepath "C:\emailAlias.txt"

    Thanks.


    Junaid Ahmed

    Friday, May 18, 2012 1:07 PM
  • Dear Fellow,

    An easy way out is that you can export the list of all your email addresses using powershell command Get-Mailbox | ft PrimarySmtpAddress | Out-File -Filepath "C:\emailAddresses.txt" and import this file in MS Excel and replace "@emaildomain.com" with blank.

    But why you want to do so? Are your email Alias different from user name or account logon names?

    If not, then you can simply export the list by using command get-mailbox | ft alias | Out-File -Filepath "C:\emailAlias.txt"

    Thanks.


    Junaid Ahmed

    Exporting to text and then manually re-importing in excel via the text import wizard, to then format in excel is way too many steps.

    There are many ways to accomplish this within PowerShell directly.

    For example, it you type:

    Get-Mailbox User1 | select -ExpandProperty primarysmtpaddress 

    You can see the email address is broken in half already.  To further only select the local part (to the left of the '@'), you can do this:

    Get-Mailbox User1 | select -ExpandProperty primarysmtpaddress | select local

    If you share what you're actually trying to accomplish we can suggest a better script, but the above is what you asked for.



    Mike Crowley | MVP
    My Blog -- Planet Technologies


    Sunday, May 20, 2012 6:17 PM
    Moderator
  • Hello SKM12,

    You can follow these to do that:

    $Address=((Get-mailbox username).Primarysmtpaddress).toString()

    $Name=$Address.Split("@")[0]

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support

    • Marked as answer by SKM12 Monday, May 21, 2012 10:56 AM
    Monday, May 21, 2012 9:39 AM
  • Hi Evan

    Your response is what I was looking for. Works fine.

    Thanks!!

    Monday, May 21, 2012 10:56 AM
  • Not bitter or anything.  ;)  But how is that the best answer?
    Monday, May 21, 2012 5:05 PM
    Moderator
  • Hi Mike, you are welcome

    When I test your solution in my script, I get "@{Local=User1}" instead "User1" as in Evan Liu solution.

    Regards!


    • Edited by SKM12 Monday, May 21, 2012 5:30 PM
    Monday, May 21, 2012 5:30 PM
  • 6 Years later..... Yours is the best answer :-)
    Thursday, December 5, 2019 10:18 AM