Author Posts

October 6, 2016 at 10:22 pm

Simple question, why would this not work:

$SelectARG = 'Name'
$SelectARG += ', Samaccountname'

get-aduser tuser | Select $SelectARG

This is just a sample of something I am trying.
$SelectARG does return: Name, Samaccountname
so I am not sure why it does not work.

Thanks,
Scott

October 6, 2016 at 10:40 pm

Try it this way:

$SelectARG = 'Name','Samaccountname'
Get-ADUser tuser | Select-Object $SelectARG

Please read the following post: How to Format Code in the Forums

  • This reply was modified 1 year, 10 months ago by  Olaf Soyk. Reason: added help

October 6, 2016 at 10:51 pm

Sorry I should have been clearer.

I separated them out because I was going to use IF statements to determine which properties I wanted returned, like:

$SelectARG = 'Name'

IF(Something) {
    $SelectARG += ', Samaccountname'
}

IF(Somethingelse) {
    $SelectARG += ', Surname'
}
    get-aduser tuser | Select $SelectARG

So I could wind up with:

get-aduser tuser | Select Name

or:

get-aduser tuser | Select Name, Surname

or:

get-aduser tuser | Select Name, Samaccountname, Surname

depending on the If statements.

Does that make sense?
Scott

October 6, 2016 at 10:54 pm

IF(Something) {
    $SelectARG = 'Name','Samaccountname'
}
IF(Somethingelse) {
    $SelectARG = 'Name','Surname'
}
get-aduser tuser | Select $SelectARG

Could you PLEASE format your code as code??

  • This reply was modified 1 year, 10 months ago by  Olaf Soyk. Reason: added content

October 6, 2016 at 11:04 pm

Sorry about that, it was not clear to me that the PRE button was the code button even through it's clearly stated 🙁

The above would work but is there no way to construct the statement using +=?

I have a gui with checkboxes for each property and I want to only show the properties for the checked properties.
I thought the If statements would be what I needed. Using the += would just give me the properties I needed.

October 6, 2016 at 11:16 pm

You will have to pass the properties you want as an array. Arrays have a fixed size when you create them. You could use following trick to 'create' a new array with the content of the old one added by something you want:

$SelectARG = 'Name','Surname'
$SelectARG = $SelectARG,'DisplayName'

October 6, 2016 at 11:17 pm

# @() is an array. 
# Force Selectarg to be a 1 element array
$SelectARG = @() + 'Name'

# add samaccountname as a element to the array
$SelectARG += 'Samaccountname'

# now that $Selectarg is an [string[]] 
# you can pass it to -property which accepts arrays
get-aduser tuser | Select -property $SelectARG

Your attempt wasn't working because you were creating a string containing commas.

# this creates a string
$SelectARG = 'Name'

# this appends a comma space into the string
$SelectARG += ', Samaccountname'

#what you wrote was getting interpreted as
Select -property 'Name, Samaccountname'

October 6, 2016 at 11:25 pm

Select(-Object) Property is expecting a string array. You can do something like this:

$props = @()
$props += "One"
$props += "Two"
$props += "Three"

$props

or

[array]$props = "One"
$props += "Two"
$props += "Three"

$props

or

$props = @("One")
$props += "Two"
$props += "Three"

$props

In your posted example, you were doing this:

$props = "One"
$props += ",Two"
$props += ",Three"

$props

This is taking a string and then doing string concatenation. If you use $props.GetType(), you'll see that the last example will show as String:

PS C:\Users\Rob> $props.GetType()


IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     String                                   System.Object

October 6, 2016 at 11:29 pm

Ahhhh, I get it now. That makes perfect sense and works great.

Thanks to all three of you, I am still learning.

Have a great night,
Scott