Powershell - How to use $_ after text

1etherer

Fully Optimized
Messages
1,878
Location
Earth
HEY (SOUL lol)

How would I include the output of $user in text e.g
Code:
$user = get-aduser -Filter * -SearchBase "DC=domain,DC=local"

get-aduser "domain\$User" | select name
so the output would be:
get-aduser "domain\testuser1"
get-aduser "domain\testuser2"
etc


Would PS just see the variable in the ""?

p.s - this isnt the script i am writing, it is an example. :cool:


UPDATE - stupid me, I just tested it and works...
 
Last edited:
This would be a better way to write it:
Code:
$users = get-aduser -Filter * -SearchBase "DC=domain,DC=local" | select name
 
This would be a better way to write it:
Code:
$users = get-aduser -Filter * -SearchBase "DC=domain,DC=local" | select name

I love PS but it's things like that -Filter parameter that annoy me. Why can't they include a list of valid things you can filter by... you have to Google it, and it's not always that easy to find an up to date result.
 
Ah right lol, well if you wanted to use $_ and pipe it, you could do it like so:

Code:
$users = get-aduser -Filter * -SearchBase "DC=domain,DC=local" | Foreach-Object {write-host "Lol this guy's name is $($_.name), who'd seriously call their kid that"}

The $_ is just a placeholder for the current variable in the loop.

When you use a pipe "|" you're passing the results of the previous command to the next command.
So in this case, we're saying "get a list of all users with this criteria" then we want to pass the result of that request to our next command, which is "for each user we find, write out their name". How do we access the current user in that loop? With the placeholder $_ variable.

Oh, just in case it's unclear too, "$_.name" is enclosed by $() because it's inside a string.
i.e. if I just wrote "The name of this user is $_.name" then it would literally print out exactly what I wrote.
By adding the $() I can tell powershell that I want it to interpret $_.name as a variable


I love PS but it's things like that -Filter parameter that annoy me. Why can't they include a list of valid things you can filter by... you have to Google it, and it's not always that easy to find an up to date result.

Open up your PS prompt and type "get-help get-aduser"

You'll notice in the 3 paragraph description, one full paragraph is dedicated to the -Filter and -LDAPFilter parameters :p and it says:
For more information about the Filter parameter syntax, see about_ActiveDirectory_Filter. If you have existing LDAP query strings, you can use the LDAPFilter parameter.
Voila, straight out says if you've already got a filter you run in AD, just whack it in to -LDAPFilter
And if we copy/paste "about_ActiveDirectory_Filter" into google and choose the first result back, we get this page: https://technet.microsoft.com/en-us/library/hh531527(v=ws.10).aspx

That's a hell of a lot of info to include for just one parameter of one command, imagine if they had to do that for all parameters of all commands.
 
Oh wow, haha. Kinda funny. That is one of the first things I learnt when I read a PS book last year. And apparently, one of the first things I forgot too :p
 
Last edited:
Back
Top Bottom