When Speed Matters

by Oct 28, 2021

PowerShell is a general-purpose automation language, so it aims to be versatile and intuitive to use. Speed is not the top priority.

If you do care for maximum speed then there are a few cmdlets that do almost exactly what .NET calls can do. Using direct .NET calls in these instances is faster, especially when these cmdlets are called often, like in a loop. The flipside is that it makes your code harder to read.

Here are a few examples:

 
# cmdlet
PS> Join-Path -Path $env:temp -ChildPath test.txt
C:\Users\tobia\AppData\Local\Temp\test.txt

# direct .NET
PS> [System.IO.Path]::Combine($env:temp, 'test.txt')
C:\Users\tobia\AppData\Local\Temp\test.txt


# cmdlet
PS> Get-Date

Monday, October 4, 2021 12:34:46

# direct .NET
PS> [DateTime]::Now

Monday, October 4, 2021 12:34:52   
 


Twitter This Tip! ReTweet this Tip!