Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Query AD for User Properties from Email Addresses in Text File

$
0
0

I'm new at Powershell and have some code that I've started that is not working. I think I have something out of place. I can pull a list of users from my entire Active Directory. But I need this script to be tweaked so that I can have the text file called "User_List.txt" that will have a list of emails like; john.doe@abccompany.com

In the text file it will be listed 1 email address per line.

 

Please HELP.....

 

Here is the code;

 

#Capture Current PATH
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path

cd $directorypath

Function Get-AllAdUsers {

$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.PageSize = 100
$ADSearch.CacheResults = $false
$ADSearch.SearchScope = "subtree"
$ADSearch.SearchRoot = "LDAP://$Domain"

$ADSearch.Filter = "(objectClass=user)"

#$ADSearch.PropertiesToLoad.Add("distinguishedName")
$ADSearch.PropertiesToLoad.Add("sAMAccountName")
$ADSearch.PropertiesToLoad.Add("userPrincipalName")

$userObjects = $ADSearch.FindAll()

#From a txt file
$RESULTS = Get-Content $PWD\User_List.txt

#For each user in TXT file
#foreach ($user in $RESULTS)

foreach ($user in $userObjects)
{

#$dn = $user.Properties.Item("distinguishedName")
$sam = $user.Properties.Item("sAMAccountName")
$upn = $user.Properties.Item("userPrincipalName")

"$sam, $upn"

}

}


Get-AllAdUsers
""
"-Seconds-"
(measure-command {$all = Get-AllAdUsers} ).TotalSeconds

""
"-Count-"
$all.Count


Viewing all articles
Browse latest Browse all 6937

Trending Articles