Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Feature Request - Where-Object speed improvements #9941

Copy link
Copy link
@PrzemyslawKlys

Description

@PrzemyslawKlys
Issue body actions

Summary of the new feature/enhancement

As it stands I've stopped using Where-Object in performance-related scenarios. I used it a lot when creating different scripts but then I found out it's actually 7-10x slower than simple foreach loop.

$UsersAll = Get-ADUser -Properties Manager, DisplayName, EmailAddress -Filter '*'
$UsersWithManagers = foreach ($User in $UsersAll) {
    $Manager = ($UsersAll | Where-Object { $User.Manager -eq $_.DistinguishedName })
    [PSCustomobject] @{
        SamAccountName = $User.SamAccountName
        Manager        = $User.Manager
        ManagerDisplay = $Manager.DisplayName
        ManagerEmail   = $Manager.EmailAddress
    }
}
$UsersWithManagers | Format-Table -AutoSize

This takes 7 minutes 54 seconds on a domain of size 4412 users. With foreach approach like this:

$UsersAll = Get-ADUser -Properties Manager, DisplayName, EmailAddress -Filter '*'
$UsersWithManagers = foreach ($User in $UsersAll) {
    $Manager = foreach ($_ in $UsersAll) {
        if ($User.Manager -eq $_.DistinguishedName) {
            $_
            break
        }
    }
    [PSCustomobject] @{
        SamAccountName = $User.SamAccountName
        Manager        = $User.Manager
        ManagerDisplay = $Manager.DisplayName
        ManagerEmail   = $Manager.EmailAddress
    }
}
$UsersWithManagers | Format-Table -AutoSize

It takes just 59 seconds. I ended up using hashtable for that particular problem (https://evotec.xyz/how-i-didnt-know-how-powerful-and-fast-hashtables-are/) but the main idea here is that Where-Object is just slow for larger loops.

It would be really cool to "fix" this in PowerShell 7 so Where-Object can once again be used as GO TO to solution for filtering stuff.

ChrisMagnuson, paule96 and mklement0

Metadata

Metadata

Assignees

Labels

Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performancecore PowerShell engine, interpreter, and runtime performance

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.