none
User Email from %UserName% RRS feed

  • Question

  • Team
    Please help me to get user email of current user using command line. This is used to trigger email to current user while executing a windows batch file.

    I tried with 

    net user "%USERNAME%" /domain | FIND /I "Full Name" 

    like this is there any way to get current user email?

    Please help

    Thanks
    Remesh

    Tuesday, January 30, 2018 12:09 PM

Answers

  • Thank You Mike for your quick support. Its working for me.
    • Marked as answer by CP Remesh Thursday, February 1, 2018 11:38 AM
    Thursday, February 1, 2018 11:38 AM
  • Thank You Bill.

    Am using cmd.

    Will use the same in PS as well for another program.

    • Marked as answer by CP Remesh Thursday, February 1, 2018 11:39 AM
    Thursday, February 1, 2018 11:39 AM

All replies

  • Are you talking about domain user?

    Get-ADUser -Identity 'username' | Select-Object UserPrincipalName


    Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.


    • Edited by Vlad817263 Tuesday, January 30, 2018 12:23 PM
    Tuesday, January 30, 2018 12:23 PM
  • Get-ADUser -Identity 'username' | Select-Object UserPrincipalName
    This only works if the Powershell Active Directory module is available!!!

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, January 30, 2018 12:28 PM
  • Yes. Its Domain User
    Tuesday, January 30, 2018 2:45 PM
  • Thanks

    Its for Domain User and need to execute in Windows Command line

    Tuesday, January 30, 2018 2:46 PM
  • Its for Windows Batch..
    Tuesday, January 30, 2018 2:49 PM
  • There is no builtin batch command or utility to get the users email.  You can use PowerShell to do this.


    \_(ツ)_/

    Tuesday, January 30, 2018 7:09 PM
  • This will return a text string to the command prompt, assuming you're wanting to pull the user's 'mail' attribute from Active Directory. 

    REM 	This is the how to build the encoded command in PowerShell
    REM 	$String = '([ADSI]("LDAP://" + (whoami /fqdn))).mail'
    REM 	$encodedcommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($string))
    
    Rem 	Now this will work in the command prompt
    powershell.exe -e KABbAEEARABTAEkAXQAoACIATABEAEEAUAA6AC8ALwAiACAAKwAgACgAdwBoAG8AYQBtAGkAIAAvAGYAcQBkAG4AKQApACkALgBtAGEAaQBsAA==


    Mike Crowley | MVP
    My Blog -- Baseline Technologies

    Wednesday, January 31, 2018 2:38 PM
  • Well, if you've got PowerShell, just use PowerShell. No need to muck about with cmd.exe at all. Here's how to get the mail attribute for the current user:


    $searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)"
    $searcher.PropertiesToLoad.AddRange(@("mail"))
    $searcher.FindOne().Properties["mail"][0]
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 31, 2018 3:19 PM
    Moderator
  • Well, if you've got PowerShell, just use PowerShell. No need to muck about with cmd.exe at all. Here's how to get the mail attribute for the current user:


    $searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)"
    $searcher.PropertiesToLoad.AddRange(@("mail"))
    $searcher.FindOne().Properties["mail"][0]
    


    -- Bill Stewart [Bill_Stewart]

    True, but he explicitly states above the requirement is to use the cmd prompt. perhaps he is editing an existing script and doesn't want to rework the whole thing.

    Mike Crowley | MVP
    My Blog -- Baseline Technologies

    Wednesday, January 31, 2018 3:30 PM
  • But your solution depends on PowerShell to work. My point is that if you already have PowerShell (and you do starting in Windows 7 and newer), just use PowerShell. Remove the unnecessary cmd.exe requirement altogether as it doesn't add anything useful.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 31, 2018 4:07 PM
    Moderator
  • Thanks

    Its for Domain User and need to execute in Windows Command line

    Powershell.exe is present on any computer since Vista. Telling him to instead run his entire code within a powershell session does not satisfy the requirement he stated above.



    Mike Crowley | MVP
    My Blog -- Baseline Technologies

    Wednesday, January 31, 2018 5:38 PM
  • I understand - I am suggesting the requirement needs to be adjusted. There's not a good reason for it.

    (FYI whoami /fqdn doesn't output an escaped distinguishedName, so this will fail if your object names contain characters that need to be escaped. My PowerShell code does not have this problem.)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 31, 2018 5:40 PM
    Moderator
  • Thank You Mike.

    REM This is the how to build the encoded command in PowerShell REM $String = '([ADSI]("LDAP://" + (whoami /fqdn))).mail' REM $encodedcommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($string)) Rem Now this will work in the command prompt powershell.exe -e KABbAEEARABTAEkAXQAoACIATABEAEEAUAA6AC8ALwAiACAAKwAgACgAdwBoAG8AYQBtAGkAIAAvAGYAcQBkAG4AKQApACkALgBtAGEAaQBsAA==

    Could you please help me understand, how we we got powershell parameter

    KABbAEEARABTAEkAXQAoACIATABEAEEAUAA6AC8ALwAiACAAKwAgACgAdwBoAG8AYQBtAGkAIAAvAGYAcQBkAG4AKQApACkALgBtAGEAaQBsAA==


    Thursday, February 1, 2018 11:28 AM
  • Thank You Mike for your quick support. Its working for me.
    • Marked as answer by CP Remesh Thursday, February 1, 2018 11:38 AM
    Thursday, February 1, 2018 11:38 AM
  • Thank You Bill.

    Am using cmd.

    Will use the same in PS as well for another program.

    • Marked as answer by CP Remesh Thursday, February 1, 2018 11:39 AM
    Thursday, February 1, 2018 11:39 AM
  • Hi Mike

    One more help

    How we can assign this PowerShell value to a String Variable in Widows batch file./

    Thursday, February 1, 2018 12:43 PM
  • Don't use a batch file. Use a PowerShell script.

    -- Bill Stewart [Bill_Stewart]

    Thursday, February 1, 2018 3:09 PM
    Moderator
  • Hello... is there a way to output the result to a text file?  Thanks!!
    Wednesday, August 22, 2018 3:10 PM