Powershell Get-Aduser

Mabrito

Supreme [H]ardness
Joined
Dec 24, 2004
Messages
7,004
I am working on a PowerShell script to get all the users from an specific defined OU and add them to the following array.

$adusernames = Get-ADUser -filter * -SearchBase "OU=TestUsers,DC=xyz,DC=com" | Select sAMAccountName

Issue im running into, is when the Get-ADuser command gets the users, it adds the users in the array with the sAMAccountName attribute.

Basically, this array is getting fed into a loop, where each element is being sent to the following query 1 by 1 to list out that users mail attribute:

$mailAttribute = (Get-ADUser $Name -properties mail).mail

For the $Name variable, powershell is feeding it the following value: sAMAccountName=username

Is there a way to just add the sAMAccountNames return value as a string value into the array?
 
I know I'm late to the game but you can use

Code:
$adusernames = (Get-ADUser -f 'enabled -eq $true' -searchbase 'OU=Location,OU=Dept,DC=.com' -Properties * | Select sAMAccountName,mail)

It will give you a 2 column listing.

User.Name (tab) Mail

From there you can export-csv or whatever

I added the part to look only for enabled accounts since we have several thousand users here at my location and a LOT of turnover so there are disabled accounts and deactivated accounts present in our OU
 
Back
Top