Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a user's email address from the username via PowerShell and WMI?

I have a user's network login name. From PowerShell and WMI is it possible to get a valid email for that user? Note that the login name is different than the name in the email, so I can't just combine the login name with the email domain.

like image 273
Michael Kelley Avatar asked Apr 17 '12 01:04

Michael Kelley


People also ask

How do I get user information in PowerShell?

Use Get-LocalUser PowerShell cmdlet to List All User Accounts. The Get-LocalUser PowerShell cmdlet lists all the local users on a device. Remember that Active Directory domain controllers don't have local user accounts.

Does WMI use PowerShell?

PowerShell ships by default with cmdlets for working with other technologies such as Windows Management Instrumentation (WMI). There are several native WMI cmdlets that exist in PowerShell without having to install any additional software or modules.


2 Answers

Here's another possible way (original source):

PS> [adsisearcher].FullName
System.DirectoryServices.DirectorySearcher

PS> $searcher = [adsisearcher]"(objectClass=user)"
PS> $searcher

CacheResults             : True
ClientTimeout            : -00:00:01
PropertyNamesOnly        : False
Filter                   : (objectClass=user)
PageSize                 : 0
PropertiesToLoad         : {}
ReferralChasing          : External
SearchScope              : Subtree
ServerPageTimeLimit      : -00:00:01
ServerTimeLimit          : -00:00:01
SizeLimit                : 0
SearchRoot               :
Sort                     : System.DirectoryServices.SortOption
Asynchronous             : False
Tombstone                : False
AttributeScopeQuery      :
DerefAlias               : Never
SecurityMasks            : None
ExtendedDN               : None
DirectorySynchronization :
VirtualListView          :
Site                     :
Container                :

PS> $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
PS> $searcher.FindOne().Properties.mail
like image 97
Michael Kelley Avatar answered Oct 26 '22 17:10

Michael Kelley


The simplest way is to useActive-Directory.

As you are using PowerShell tag and not PowerShell V2.0 you can use ADSI.

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://WM2008R2ENT:389/dc=dom,dc=fr","[email protected]","Pwd")

# Look for a user
$user2Find = "user1"
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$rc = $Rech.filter = "((sAMAccountName=$user2Find))"
$rc = $Rech.SearchScope = "subtree"
$rc = $Rech.PropertiesToLoad.Add("mail");

$theUser = $Rech.FindOne()
if ($theUser -ne $null)
{
  Write-Host $theUser.Properties["mail"]
}

You can also use userPrincipalName instead of sAMAccountName in the filter, for userPrincipalName you can use user@domain form.


Using WMI : If you absolutly want to do it with WMI.

$user2Find = "user1"
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail

You can use the second solution localy on your server or from a computer inside the domain, but it's a bit more complicated to authenticate to WMI from outside the domain.


Using PowerShell 2.0

Import-Module activedirectory
$user2Find = "user1"
$user = Get-ADUser $user2Find -Properties mail
$user.mail
like image 32
JPBlanc Avatar answered Oct 26 '22 17:10

JPBlanc