Kahn Kah
Kahn Kah

Reputation: 1453

Use a field of a variable inside of a foreach in PowerShell

So I have a list of Active Directory users:

$users = Get-AdUser -Filter {Enabled -eq "True"}

What I want to do is group them based on their description since you have 3 possible descriptions in the whole user list.

However: I can't even seem to even use the Descriptionfield inside of a foreach:

foreach ($user in $users)
{
    Write-Host $user.Name    
    Write-Host $user.Description
}

Their name shows but not their description.

Why is this?

Upvotes: 2

Views: 360

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23355

Get-ADUser only returns specific properties by default. The description property is not one of these. To ensure it is returned you need to use the following parameter with Get-ADUser:

-Properties Description

"This cmdlet retrieves a default set of user object properties. To retrieve additional properties use the Properties parameter." - https://technet.microsoft.com/en-gb/library/ee617241.aspx

Upvotes: 4

Related Questions