Skip to content

Instantly share code, notes, and snippets.

@paulschmeida
Created November 6, 2019 23:30
Show Gist options
  • Save paulschmeida/8120dee7a626111f8ffc093365f6bc4e to your computer and use it in GitHub Desktop.
Save paulschmeida/8120dee7a626111f8ffc093365f6bc4e to your computer and use it in GitHub Desktop.
Comparison of filtering early and where-object while using Get-CimInstance
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}"
@paulschmeida
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment