Brian McMahon
Brian McMahon

Reputation: 399

PowerShell performance issue with Where-Object

Is this behavior explainable?

PS C:\> $PosixGroups = Get-ADGroup -LDAPFilter '(ObjectClass=posixGroup)' -Server $DomainController -Credential $cred -Properties gidNumber
PS C:\> $gid=500
PS C:\> $posixGroups.count
1049
PS C:\> Measure-Command {$found = $PosixGroups | Where-Object {$_.gidNumber -eq $gid}}

Days              : 0
Hours             : 0
Minutes           : 1
Seconds           : 42
Milliseconds      : 939
Ticks             : 1029394783
TotalDays         : 0.00119142914699074
TotalHours        : 0.0285942995277778
TotalMinutes      : 1.71565797166667
TotalSeconds      : 102.9394783
TotalMilliseconds : 102939.4783


PS C:\> Measure-Command {$found = $PosixGroups | Where-Object {$_.gidNumber -eq $gid}}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 14
Ticks             : 141895
TotalDays         : 1.64230324074074E-07
TotalHours        : 3.94152777777778E-06
TotalMinutes      : 0.000236491666666667
TotalSeconds      : 0.0141895
TotalMilliseconds : 14.1895

Terrible performance at 1 minute 42 seconds the first time, then a subsequent repeat of the command completes in 14 milliseconds? I don't understand this, I haven't noticed anything like this before using Where-Object.

Upvotes: 0

Views: 80

Answers (0)

Related Questions