POWERSHELL OPTIMIZATION FOR SPEED Tim Curwick Kirk Munro MadWithPowerShell.com @poshoholic [email protected] Director of Learning Solutions DevOps, Azure, Automation Consultant at RBA, Inc. Learn on Demand Systems Tim Curwick @MadWPowerShell Microsoft Computer My PowerShell MVP nerd for 35 years favorite food is cows Kirk Munro @Poshoholic 10-time ~20 PowerShell MVP years building IT solutions Organic, paleo, healthy BALANCE BALANCE Minimize speed what needs to be optimized for PRECOMPILING VS JIT COMPILING PRECOMPILING VS JIT COMPILING TESTING Test Test again Test again PROFILES Don’t use profiles MODULES Avoid modules FUNCTIONS Code in a function is faster FUNCTIONS Code in a function is faster Using more than one function is slower NATIVE CLASS DEFINITION Code in a class definition is faster .NET METHODS .Net methods are fast PIPELINES Don’t use pipelines LOOPS LOOPS Loop Pipe to ForEach-Object Pipe to Measure-Object .ForEach() method For loop ForEach loop Relative time taken 14.2 7.0 4.8 2.3 1.0 LOOPS Loop Pipe to ForEach-Object Pipe to Measure-Object .ForEach() method For loop ForEach loop Relative time taken PS 5.0 PS 5.1 14.2 7.0 4.8 2.3 1.0 38.0 1.7 35.7 2.1 0.9 LOOPS Loop Pipe to ForEach-Object Pipe to Measure-Object .ForEach() method For loop ForEach loop Relative time taken PS 5.0 PS 5.1 5.1+log 14.2 7.0 4.8 2.3 1.0 38.0 1.7 35.7 2.1 0.9 56.5 6.8 43.0 2.0 0.9 BREAK/CONTINUE NORMALIZE CALCULATIONS Do not repeat anything that does not need to be repeated SIMPLIFY CALCULATIONS SIMPLIFY CALCULATIONS 3x3=? SIMPLIFY CALCULATIONS 3x3=? 3.14159265 = ? SIMPLIFY CALCULATIONS Avoid floating point operations Avoid more complex operations AVOID UNUSED CODE AVOID UNUSED CODE Avoid comments Avoid Write-Debug, Write-Verbose, Write-Error Avoid unnecessary tests
© Copyright 2026 Paperzz