Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass Powershell Get-ADUser -Properties in a variable?

I'd like to do something like this, mainly for code readability:

$ADProperties = "extensionAttribute1,Department,Company,telephoneNumber"
$ADFilter = "{(objectClass -eq "user") -and (enabled -eq $True)}"

Get-ADUser -Filter $ADFilter -Properties $ADProperties

I get errors like: Get-Aduser : One or more properties are invalid.

Seems like I've seen a way to pass either the Filter or Properties somehow, but can't find it now.

EDIT - This works for -Properties:

$ADProperties = "EmployeeID","EmployeeNumber","extensionAttribute1","Department"

(Thanks Eris) but this throws error:

$ADProperties = extensionAttribute1,Department,Company,telephoneNumber 

Also, per Vasili, this works:

$str = @("foo","bar")

This works for -Filter

$ADFilter = {(objectClass -eq "user") -and (enabled -eq $True)}

Thanks everyone. MOB

like image 242
MOB Avatar asked Oct 19 '25 09:10

MOB


2 Answers

The Properties parameter takes a string[] (array) data type, as such, it is expecting something more like this:

@("extensionAttribute1","Department","Company","telephoneNumber")

like image 102
Vasili Syrakis Avatar answered Oct 22 '25 04:10

Vasili Syrakis


You can do it if you construct your command in a String Variable like this:

$cmd = Get-ADUser -Filter $ADFilter -Properties $ADProperties

And then invoke it

invoke-command $cmd
like image 44
Paul Avatar answered Oct 22 '25 05:10

Paul