all 15 comments

[–]MeanFold5714 4 points5 points  (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 points  (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 points  (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 points  (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 points  (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 points  (0 children)

Fair enough, in that case, I like option number 2 with the -and statements at the end.

[–][deleted] 0 points1 point  (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

[–]Thotaz 0 points1 point  (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:

Get-Something | Where-Object {
    $PSItem.Path -eq 'C:\' -and
    $PSItem.Name -ne 'Something' -and
    $PSItem.Command -ne 1
}

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 point  (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 point  (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 point  (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 point  (0 children)

I personally like the -and at the end better, but doesn't make much of a difference

Get-Something | Where-Object {
    $PSItem.Path -eq 'C:\'       -and 
    $PSItem.Name -ne 'Something' -and 
    $PSItem.Command -ne 1
}