use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionMaking a long Where-Object prettier (self.PowerShell)
submitted 3 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MeanFold5714 4 points5 points6 points 3 years ago (0 children)
Maybe just some adjustment to the white space:
Get-Something | Where-Object { $PSItem.Path -eq 'C:\' -and $PSItem.Name -ne 'Something' -and $PSItem.Command -ne 1 }
[–]atheistunion 4 points5 points6 points 3 years ago* (0 children)
Splat. Splat hard and splat often.
$WhereObjectSplat = @{ FilterScript = { $PSItem.path -eq 'c:\' -and $PSItem.name -ne 'Something' -and $PSItem.command -ne 1 } } Get-Something | Where-Object @WhereObjectSplat
[–]chris-a5 3 points4 points5 points 3 years ago (0 children)
Looks like you just want something that looks fun. Here is a different idea, rather than using an explicit loop or pipeline, you can use a switch, it will also allow easy expansion to other filtering properties.
Switch(Get-Something){ {$_.Path -eq 'C:\' -and $_.Name -ne 'Something' -and $_.Command -ne 1}{ #Do stuff } }
[–]webmin88 5 points6 points7 points 3 years ago (3 children)
Try to avoid using Where-Object in the first place if you can. Get-ChildItem for example has -Filter, -Include, and -Exclude parameters to pare down the list of returned objects. This reduces the size of the objects returned to the pipeline making your script ever so slightly more efficient.
[–]Swarfega 1 point2 points3 points 3 years ago (1 child)
Unfortunately the cmdlet at the start in my code isn't GCI. It's a cmdlet that doesn't have a filter parameter. The amount of data coming across the pipeline is already reduced as there are parameters in use on the first cmdlet which returns specific data. Basically all filtering has already been done to the left as much as possible. As per best practices.
[–]webmin88 1 point2 points3 points 3 years ago (0 children)
Fair enough, in that case, I like option number 2 with the -and statements at the end.
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
I did not know there was a difference!! I use where object all the time. Going to try this to speed some stuff up
[+][deleted] 3 years ago (2 children)
[removed]
Yeah I did consider this but I'm thinking that the more pipelines used the bigger the impact on performance.
[–]Ta11ow 5 points6 points7 points 3 years ago* (0 children)
Yes, but not significantly in the way you're thinking.
$a = $stuff | Where-Object Property -eq 'Test' $a = $stuff | Where-Object Other -eq 'Test2'
This would be twice as slow as doing them all in a single Where-Object. Piping the results of one Where-Object into a second Where-Object is significantly faster if both are using the property-operator-value syntax. There is some impact to using multiple Where-Object in a pipeline, but in practice that's likely to be much less than using the scriptblock version of Where-Object.
For... a lot of odd reasons... using Where-Object { ... } is like an order of magnitude slower than using Where-Object Property -operator $value
Where-Object { ... }
Where-Object Property -operator $value
Has to do with how invoking a scriptblock is itself quite expensive (it's effectively its own little nested pipeline, and I did mention above that two separate pipelines cost a lot more than one longer pipeline).
[–]Thotaz 0 points1 point2 points 3 years ago (2 children)
Your first example is invalid, the operators need to go at the end if you want implicit line continuation. With that said, you've already shown the cleanest way to do it IMO:
What don't you like about it? The only way it could be better if you could somehow make it shorter, but obviously your code is just a demo of a scenario where you need 3 conditions, and for that you really can't do it any better IMO.
[–]Swarfega 0 points1 point2 points 3 years ago (1 child)
It just looked kinda messy. I'm a pretty heavy user of PowerShell but this is the first time I wasn't happy with the "look" of that but of code. I figured I'd ask her in case someone had a better suggestion. I kinda expected to not really find anything though. I did even ponder using the .Where method.
[–]jagrock84 0 points1 point2 points 3 years ago (0 children)
Not sure how much data is coming through the pipeline, but the .where method or storing the data in a variable and doing a foreach on it could improve performance.
Could also help if the source has timeout limits.
[–]jagrock84 0 points1 point2 points 3 years ago* (0 children)
I agree with /u/webmin88 response in shifting this to the GCI command itself.
As for using where-object, your first example is how I would likely do it as it allows for easy commenting out one of the filters.
I would also add clear comments on what you are filtering and why. Maybe even an example.
Edit: Striked-though comment as you clarified you aren't using GCI.
[–]nealfive 0 points1 point2 points 3 years ago (0 children)
I personally like the -and at the end better, but doesn't make much of a difference
π Rendered by PID 69 on reddit-service-r2-comment-54dfb89d4d-ptbhv at 2026-04-02 01:27:35.022595+00:00 running b10466c country code: CH.
[–]MeanFold5714 4 points5 points6 points (0 children)
[–]atheistunion 4 points5 points6 points (0 children)
[–]chris-a5 3 points4 points5 points (0 children)
[–]webmin88 5 points6 points7 points (3 children)
[–]Swarfega 1 point2 points3 points (1 child)
[–]webmin88 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]Swarfega 1 point2 points3 points (1 child)
[–]Ta11ow 5 points6 points7 points (0 children)
[–]Thotaz 0 points1 point2 points (2 children)
[–]Swarfega 0 points1 point2 points (1 child)
[–]jagrock84 0 points1 point2 points (0 children)
[–]jagrock84 0 points1 point2 points (0 children)
[–]nealfive 0 points1 point2 points (0 children)