locked
Using a variable in a get-aduser command RRS feed

  • Question

  • Get-ADUser -Filter * -SearchBase "PATH_TO_MY_OU" -Properties name, department, manager, company, streetaddress, city, state, postalcode | ft name, department, manager, company, streetaddress, city, state, postalcode

    I'm playing around with pulling user account information from a given OU via powershell.  Forgive my "noobness" when it comes to ps.  The above command works just fine for me and I thought to myself, self...  I wonder if we could store the "-Properties and ft" info in a variable then just use the variable.  So I tried this.

    $properties = 'name, department, manager, company, streetaddress, city, state, postalcode'
    Get-ADUser -Filter * -SearchBase "PATH_TO_MY_OU" -Properties $properties | ft $properties

    I get a "one or more properties are invalid" error.

    I've tried running it without piping to format table, setting -Properties to *, and putting $properties in quotes.  Can you not pass the properties using a variable like this?  

    Tuesday, November 27, 2018 8:59 PM

Answers

  • You are defining the $properties variable as a single string with embedded commas and spaces. It is not an array (list of comma-separated items) like in your first command.

    To get it to work that way, use an array, not a string. Example:


    $properties = "name","department","manager","company","streetaddress","city","state","postalcode"
    Get-ADUser -Filter * -SearchBase "PATH_TO_MY_OU" -Properties $properties | ft $properties

    If you want to export as CSV, replace ft $properties with Select-Object $properties | Export-Csv ... .


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by IndyBeerGuy Tuesday, November 27, 2018 9:39 PM
    Tuesday, November 27, 2018 9:11 PM

All replies

  • Simple and easy:

    $properties = 'name, department,manager,company,streetaddress,city,state,postalcode' -split ','

    $properties need to be an array of values and not a string.


    \_(ツ)_/



    • Proposed as answer by jrv Tuesday, November 27, 2018 10:20 PM
    • Edited by jrv Tuesday, November 27, 2018 10:41 PM
    Tuesday, November 27, 2018 9:06 PM
  • You are defining the $properties variable as a single string with embedded commas and spaces. It is not an array (list of comma-separated items) like in your first command.

    To get it to work that way, use an array, not a string. Example:


    $properties = "name","department","manager","company","streetaddress","city","state","postalcode"
    Get-ADUser -Filter * -SearchBase "PATH_TO_MY_OU" -Properties $properties | ft $properties

    If you want to export as CSV, replace ft $properties with Select-Object $properties | Export-Csv ... .


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by IndyBeerGuy Tuesday, November 27, 2018 9:39 PM
    Tuesday, November 27, 2018 9:11 PM
  • Thank you for the quick replies!!  All worked like a charm.  Thank you for the detailed response Bill!!
    Tuesday, November 27, 2018 9:45 PM
  • Simple and easy:

    $properties = 'name, department, manager, company, streetaddress, city, state, postalcode' -split ','

    $properties need to be an array of values and not a string.


    \_(ツ)_/


    Note: Properties should be comma-separated without the space in this case :)  

    Note that properties string is being "-spit" into an array.  This is a fast way to type a list of properties and end up with an array without having to type dozens of quotes correctly.


    \_(ツ)_/

    Tuesday, November 27, 2018 10:20 PM
  • If you want to use -split, remove the spaces from the string or say -split ', ' (including the space).

    -- Bill Stewart [Bill_Stewart]

    Tuesday, November 27, 2018 10:33 PM
  • Hard to see the spaces.  I type the strings without spaces.

    Yes.  The extra space will cause the properties to be ignored with no error,

    It would be safer to do this in case spaces are not consistent:

     $properties = 'name, department,manager,company,streetaddress,city,state,postalcode' -replace ' \s+ -split ','


    \_(ツ)_/





    • Edited by jrv Tuesday, November 27, 2018 10:49 PM
    Tuesday, November 27, 2018 10:41 PM
  • If you have a very long list that needs to be maintained then this is an easy way to do it.

    $properties = @'
    name
    department
    manager
    company
    streetaddress
    city
    state
    postalcode
    '@ -split '\n'
    This makes the list easier to edit.


    \_(ツ)_/

    Tuesday, November 27, 2018 10:48 PM