r/PowerShell • • Apr 04 '26

Powershell Noob

Hey all

I’m a slight newbie and landed a JR infrastructure engineer role that includes looking after cloud environments, patching software and machines.

Is there any advice where I could get into learning more powershell scripting or and decent YouTube courses I could follow

Any help is appreciated

20 Upvotes

49 comments sorted by

View all comments

Show parent comment

1

u/TofuBug40 Apr 06 '26

You can even notice that switching to using the Property parameter set vs the FilterScript parameter set has a not insiginficant difference.

This can and does scale out even worse when you are dealing with pulling data from systems that might be separated by geographic locations or network distance. You quickly start to notice your previously speedy approaches dragging to a crawl.

It also brings up another possibility and really the correct answer in these cases filter BEFORE not after. Almost every system that is designed to be queried, File systems, Active Directory, SQL, etc has means to give IT your filter and IT does the filtering where it lives where it has all the cores and memory and disk space to literally fly through that filter and then as an additional bonus it has LESS data it needs to send you back.

So the final test I ran didn't even use where of any kind it just added the -Filter '*.txt' parameter to the otherwise unchanged Get-ChildItem splat. For that we get

Ran 10 tests for PreFilter.
Results:
Count : 10
Average : 22.34616977
Sum :
Maximum : 22.5040598
Minimum : 22.2449309
Property : TotalSeconds

Nearly twice as fast in this example just by filtering first.

Code blocks as promised

$MeasureCommandWhereMethod =
    @{
        Expression =
            {
                Push-Location @PushLocation
                (
                    Get-ChildItem @GetChildItem
                ).
                    Where(
                        $WhereFilter               
                    )
                Pop-Location
            }
    }
$RunTestsMeasureWhereMethod = 
    @{
        TestName =
            'Where()'
        MeasureCommandSplat =
            $MeasureCommandWhereMethod 
        NumberOfTests =
            $Tests
    }
Run-Tests @RunTestsMeasureWhereMethod


$MeasureCommandWhereObjectFilter =
    @{
        Expression =
            {   
                Push-Location @PushLocation
                $WhereObject =
                    @{
                        FilterScript =
                            $WhereFilter
                    }
                Get-ChildItem @GetChildItem |
                    Where-Object @WhereObject
                Pop-Location
            }
    }
$RunTestsMeasureWhereObjectFilter = 
    @{
        TestName =
            'Where-Object -FilterScript'
        MeasureCommandSplat =
            $MeasureCommandWhereObjectFilter
        NumberOfTests =
            $Tests
    }
Run-Tests @RunTestsMeasureWhereObjectFilter

$MeasureCommandWhereObjectProperty =
    @{
        Expression =
            {
                Push-Location @PushLocation
                $WhereObject =
                    @{
                        Property =
                            'Extension'
                        EQ =
                            $true
                        Value =
                            'txt'
                    }
                Get-ChildItem @GetChildItem  |
                    Where-Object @WhereObject
                Pop-Location
            }
    }
$RunTestsMeasureWhereObjectProperty = 
    @{
        TestName =
            'Where-Object -Property -EQ -Value'
        MeasureCommandSplat =
            $MeasureCommandWhereObjectProperty
        NumberOfTests =
            $Tests
    }
Run-Tests @RunTestsMeasureWhereObjectProperty

$MeasureCommandPreFilter =
    @{
        Expression =
            {
                Push-Location @PushLocation
                $GetChildItemFiltered =
                    @{
                        File =
                            $true
                        Recurse =
                            $true
                        Filter =
                            '*.txt'
                        ErrorAction =
                       [System.Management.Automation.ActionPreference]::SilentlyContinue
                    }
                Get-ChildItem @GetChildItem
                Pop-Location
            }
    }
$RunTestsMeasurePreFilter = 
    @{
        TestName =
            'PreFilter'
        MeasureCommandSplat =
            $MeasureCommandPreFilter
        NumberOfTests =
            $Tests
    }
Run-Tests @RunTestsMeasurePreFilter