1

I'm more of a Linux bash shell guy and am running up against something in PowerShell that is puzzling me. I want to extract a list of user names from AD in a foreach loop and perform a certain operation on them.

At this point I'm simply concerned about whether I'm pulling the correct string before I run any important operation based on that string. Here is what I'm running as a test (details in square brackets removed for privacy):

Import-Module ActiveDirectory
foreach ($x in (Get-ADUser -Filter * -Server "[host]" -SearchBase '[mybase]' | Select-Object SamAccountName)) {
    Write-Output "Alias is: $x"
}

So the deal is, if I leave off the double quotes in the Write-Output statement it looks great and will print:

Alias is:
username1
Alias is:
username2
Alias is:
username3

It injects a newline before each value though, which I'd rather remove/strip from the string.

But inside of double quotes I instead get:

Alias is: @{SamAccountName=username1}
Alias is: @{SamAccountName=username2)
Alias is: @{SamAccountName=username3}

I'm not sure how to extract the value I'm looking at from this. What am I looking at here? Is this something I need to dereference, or something in an array I need to extract? Or is my variable value actually an object and not a string and I need to somehow do a ToString() operation on it (or equivalent)?

6
  • 3
    Select-Object SamAccountName -> Select-Object -Expand SamAccountName Commented Jun 21, 2019 at 16:47
  • Ah, so indeed it is an object (as I should have inferred from the command Select-Object), and expand pulls the string out of it. It's always something simple. Thanks! Commented Jun 21, 2019 at 16:50
  • I don't actually think this is a duplicate as it is specific to Select-Object and the linked-to alternate question does not so much as mention the Expand parameter, potentially confusing other viewers who happen upon this question. I can see how the output notation overlaps, but nothing else. I mean, I got my answer and am happy with it, but you may want to think about what exactly was a duplicate and how I would have found that other thread using the search feature on this site prior to posting my own (which I did). If someone types "at curly bracket" on Google mine will likely be indexed. Commented Jun 24, 2019 at 17:19
  • The problem described in that question is exactly the same as yours, and the answers, particularly the one from mklement0, describe the solution to that problem. Commented Jun 24, 2019 at 19:14
  • 1
    Yes. Parameter names in PowerShell can be shortened as long as they remain unique. -ExpandProperty is the same as -ExpandPr, or -Expand, or even -Exp. However, -Ex or -E will not work, b/c there's another parameter named -ExcludeProperty. Commented Jun 25, 2019 at 15:10

1 Answer 1

4

The issue is here:

Select-Object SamAccountName

it returns an object with SamAccountName property. If you want the value only, use
-ExpandProperty:

Select-Object -ExpandProperty SamAccountName
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.