Created
November 6, 2019 23:30
-
-
Save paulschmeida/8120dee7a626111f8ffc093365f6bc4e to your computer and use it in GitHub Desktop.
Comparison of filtering early and where-object while using Get-CimInstance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-WindowsLicenseStatus { | |
$Filter = "Name like 'Windows%' and PartialProductKey like '%'" | |
$LicenseStatus = (Get-CimInstance SoftwareLicensingProduct -Filter $Filter -Property LicenseStatus).LicenseStatus | |
return "LicenseStatus=${LicenseStatus}" | |
} | |
$exp1 = {Get-WindowsLicenseStatus} | |
$exp2 = {Get-CimInstance SoftwareLicensingProduct -Filter "Name like 'Windows%'" | where { $_.PartialProductKey } | select -ExpandProperty LicenseStatus | Write-Host} | |
$result1 = (Measure-Command -Expression $exp1).TotalSeconds | |
$result2 = (Measure-Command -Expression $exp2).TotalSeconds | |
Write-Host "Time without where-object and piping: ${result1}" | |
Write-Host "Time with where-object and piping: ${result2}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results:
Time without where-object and piping: 0.6428818
Time with where-object and piping: 22.2327831
It's interesting to see that filtering early if roughly 35x faster in this case and yet Where-Object and piping seem to be preferred way of coding by most Powershell devs, even though it kills performance.