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