0

When writing commands on powershell sometimes the output is propagated one way and sometimes the other way. Let's say I am using the command

Get-ADUser -Filter * | ? {$_.name -eq "$name"}

in that situation I can use the Where-Object as below, and sometimes there are commands the populating the results other way and for some reason the Where-Object is not working.

After investigating a little bit I found that Select-String will work for me for the other command that ran instead of the Where-Object.

So it got me wondering what could be the differences between these two outputs?

Thank you

And by the way, if someone has a good learning site that can teach me the basics of all powershell from more inside perspective like what the difference between array and hash table, that that i know already, not really basic stuff :)

2
  • Can you provide a reproducible example of a command with which you can't use Where-Object but where Select-String seems to due the trick? Commented Jun 4, 2018 at 21:22
  • Hmm... apparently i managed to use Where-Object also on the command that i couldn't before :) Commented Jun 6, 2018 at 7:25

2 Answers 2

2

Where-Object works on a collection of objects returning those objects that match a given criteria. Select-String works on a collection of strings returning those strings that match a given expression. They're similar in that they allow you to filter a collection of stuff. The stuff you can filter for one is a little different than the other. Where-Object can filter objects based on string properties or properties that cast into strings. It can also apply filters using math operators (greater than, less than, modulus etc.) to numeric properties, object comparisons, etc.

Let’s say I’ve got a file named .gitignore. If I wanted to see lines that included the phrase "paket", I'd write something like this:

cat .gitignore | select-string 'paket'

Select-String finds me the line or lines with the phrase I'm looking for. I can do the same thing with Where-Object:

cat .gitignore | where { $_ -like '*paket*' }

In this case, the object being filtered just happens to be a string so Select-String and Where-Object look pretty similar.

Now, let's create a list of dates...

$stuff = 1..10 | foreach { $(Get-Date).AddHours($(Get-Random -Minimum 1 -Maximum 25) * -1) }

We’ll filter the list of dates by hour, where hour is greater than 18 (6 PM):

$stuff | Where-Object { $_.Hour -gt 18 }

In this example, we applied a math expression (greater than) to an object's property. This is something Select-String wouldn't do. Where-Object will also work on values that don't cast to a string.

Hope this helps a little.

Sign up to request clarification or add additional context in comments.

2 Comments

First of all thank you for your clarification, that basically what I needed to know. But there is one script line that I don't really understand: $stuff = 1..10 | foreach { $(Get-Date).AddHours($(Get-Random -Minimum 1 -Maximum 25) * -1) } I know how to use foreach and variables, but can you please explain me that chunk: $(Get-Date).AddHours($(Get-Random -Minimum 1 -Maximum 25) * -1) * Why are you adding $ before the command? * And what is * sign means in powershell? Thank you. Apparently I can't do line breaker for some reason, so sorry about that :(
The asterisk (*) is the multiplication operator in Powershell. The bit with the dollar sign and parenthesis is a subexpression: blogs.technet.microsoft.com/stefan_stranger/2013/09/25/…. I'm telling Powershell execute the command or expression inside the parenthesis and provide its output to the containing command.
0

OK, Apparently I managed to also use the Where-Object to filter a command results. It's because my result was a 3 strings in every row, so using "-eq" wasn't working and giving me a blank result. I changed it to -match or -like ** and it did find the row that had the string I was looking for.

For more clarification, the command I was using is "net users /domain" - it outputs the results with 3 strings in every row.

So thank you for you answers :)

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.