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)?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 3
    `Select-Object SamAccountName` -> `Select-Object -Expand SamAccountName` – Ansgar Wiechers Jun 21 '19 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! – SeligkeitIstInGott Jun 21 '19 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. – SeligkeitIstInGott Jun 24 '19 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. – Ansgar Wiechers Jun 24 '19 at 19:14
  • Oh, interesting, I see that mklement0 mentions -ExpandProperty and not -Expand. I'm guessing the latter is a shorter form of the former. I see what you're saying now though. – SeligkeitIstInGott Jun 25 '19 at 15:02
  • 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`. – Ansgar Wiechers Jun 25 '19 at 15:10

1 Answers1

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
mklement0
  • 382,024
  • 64
  • 607
  • 775
Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34