How to obtain email of the logged in user in power

2019-04-21 05:01发布

问题:

Using Vbscript , we are getting current user email id. It is as simple as with following lines.

Option Explicit

Dim objUser, objADSysInfo

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
WScript.Echo objUser.Mail

How to achieve the same using powershell?

回答1:

PS> $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
PS> $searcher.FindOne().Properties.mail


回答2:

I'd be concerned that the given answer doesn't use the fully qualified name. Granted that won't be a problem in most cases, but the method I'm using is as follows:

([adsi]"LDAP://$(whoami /fqdn)").mail