During the lab runs for 1.1.0, Remove-OrphanedSid over 302 objects on my lab DC took 291 seconds. That’s about a second per object on a healthy domain controller with the directory on the same machine. On Windows PowerShell 5.1, the shell the module is built for, 10,000 objects would take around three hours. That’s longer than I’d want a nightly audit to run. Version 1.0.1 was no faster. The module had always been this slow.

I picked 10,000 because it’s on the low end of the directories I’ve managed, and I wanted a run that size to stay manageable.

Issue #111 profiled one object, a user with the default 24 access rules, on 5.1 with the SID cache already warm:

StepCost
Get-Acl on the object~10 ms
Detection on the object978 ms
Per access rule~46 ms
One Write-StructuredLog at Debug, discarded at the default level4.8 ms

Reading the ACL took 10 ms. Everything after it took 968 ms.

Formatting a line, then throwing it away

Detection writes four or five Debug lines per access rule, plus a few per object. At the default Information level, none of them goes anywhere. In 1.0.1, Write-StructuredLogEntry did all its work before it asked whether the line was wanted:

1
2
3
4
# Create structured log entry
$logEntry = [PSCustomObject]@{
    Timestamp = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffK"
    Level = $Level

It then formatted a second timestamp, built the file line, called Get-Command twice and read the logging state. Only after all that did it check the level:

1
2
3
4
$writeToFile = $true
if (Get-Command Test-LogLevel -ErrorAction SilentlyContinue) {
    $writeToFile = Test-LogLevel $Level
}

At 4.8 ms a line and four or five lines a rule, discarded logging was about half of each object’s time. The PSCustomObject wasn’t even used.

1.1.0 asks first. A line has a destination if the file level wants it, or if it’s a Debug or Verbose line and that stream is turned on. That second test is the one Write-Debug and Write-Verbose make themselves:

1
2
3
4
5
6
# Most entries detection writes are Debug lines that go nowhere. Formatting one anyway cost
# several milliseconds, and detection writes several per access rule, so it took about half of
# each object's time (#111). An entry with no destination stops here.
if (-not $writeToFile -and -not $showOnConsole) {
    return
}

Nothing changes in what you see at Information and above. At -LogLevel Debug the log gets shorter, for a reason covered in the next section.

Turning SIDs into names and back

The second cost was in how detection read the rules. 1.0.1 used the ACL’s Access property:

1
2
3
4
5
$accessRules = if ($IncludeInherited) {
    $acl.Access
} else {
    $acl.Access | Where-Object { -not $_.IsInherited }
}

Access hands back every rule with its identity translated to an account name. Detection works on SIDs, so for every rule it called Resolve-IdentityReference to turn the name back into a SID. On the DC this week, I read one user’s 50 rules both ways:

1
2
Access IdentityReference type: NTAccount NT AUTHORITY\SELF
GetAccessRules IdentityReference type: SecurityIdentifier S-1-5-10

The .NET side wasn’t the slow part. Reading Access took about 1.1 ms for all 50 rules on 5.1, and asking for SIDs took 0.5 ms. In the profile, the module’s resolver took 13 ms per rule to undo a translation detection never needed. 1.1.0 reads the rules once, with SID references:

1
2
3
4
5
# Access is a computed property that translates every entry's SID to an account name on each
# read, and detection translated each name back to a SID. Reading the rules once, with SID
# references, removed both lookups from every rule (#111). An orphaned SID arrives as a
# SecurityIdentifier either way.
$allRules = if ($acl) { @($acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])) } else { @() }

A rule that comes in as a SecurityIdentifier skips the resolver entirely. The fallback paths keep Access, because a deserialized security descriptor has no GetAccessRules method.

The third cost was 20 ms per rule in Test-SIDValidityAndType, which checks a SID’s format and whether it’s well-known or protected. The same few dozen SIDs show up on every object in a domain, so 1.1.0 checks each one once per run:

1
2
3
4
if ($null -ne $script:SIDClassificationCache -and $script:SIDClassificationCache.ContainsKey($sidString)) {
    $isCandidate = $script:SIDClassificationCache[$sidString]
} else {
    $isCandidate = Test-SIDValidityAndType -SIDString $sidString -CorrelationId $CorrelationId

The cache lives for one session. Start-OrphanedSidSession creates it and Stop-OrphanedSidSession drops it, because the answer depends on the run’s configuration: a config file can change the protected and well-known lists. That’s also why the Debug log is shorter. The resolver’s two lines per rule are gone, and the format and well-known lines show up once per SID.

The numbers

From the pull request, detection alone on the same 24-rule user:

Windows PowerShell 5.1PowerShell 7
Before941 ms327 ms
After96 ms72 ms

On 5.1 that’s close to tenfold. A whole run is less dramatic, because discovery, setup and the summary cost what they cost. This week I ran Find-OrphanedSid over the entire lab domain with the 1.0.1 and 1.1.0 packages, each in a fresh process after a short warm-up run on the test OU:

5.1, 1.0.15.1, 1.1.07, 1.0.17, 1.1.0
303 objects77.2 s13.3 s25.0 s11.1 s
Per object255 ms44 ms82 ms37 ms
Results2222

The 303 objects in the lab domain aren’t all like that test user, so the per-object figures are lower on both sides. On 5.1 the run is almost six times faster. On 7 it’s a bit over twice as fast, because PowerShell 7 was already doing the expensive parts faster. The two releases differ in more than these three changes, so this table compares 1.0.1 with 1.1.0, not one pull request with its base.

Tests that throw if the work comes back

The issue asked for a benchmark test to pin the per-object cost. The pull request went another way. A timing threshold on a shared CI runner fails whenever a neighbor’s job is busy. Instead, the tests check that the expensive work doesn’t happen at all:

1
2
3
4
5
6
It "Formats nothing for a <LevelName> entry the file level filters and the console stream does not show (#111)" -ForEach @(
    @{ LevelName = 'Debug' }
    @{ LevelName = 'Verbose' }
) {
    Mock Test-LogLevel { $false }
    Mock Get-Date { throw 'an entry with no destination should not be formatted' }

If anyone moves the formatting back above the level check, Get-Date throws and the test fails, on any machine and at any load. The other new tests do the same for the cache and the rules. The cache has to check each SID once, and the rules have to arrive as SecurityIdentifier and skip the resolver.

Reading the results back once

The end of a run had its own waste. Results are written to batch files as the run goes, so a big search base doesn’t hold everything in memory, and GetAllResults() reads them back. In 1.0.1 it added each batch to an array with +=:

1
2
$batchContent = Get-Content $batchFile -Raw | ConvertFrom-Json
$allResults += $batchContent

+= on an array copies all of it every time, so the cost grows with the square of the result count, and Windows PowerShell 5.1 has no faster path for it. With -OutputPath, Find-OrphanedSid called it once to emit the results and ExportToCsv() called it again, so every batch file was read and parsed twice. 1.1.0 keeps the results it has read in a list and remembers how many batch files that covers:

1
2
3
# Only batch files flushed since the last call are read; the rest are already in the list
$batchFiles = @($this.Summary.BatchFiles)
for ($index = $this.ReadBackBatchCount; $index -lt $batchFiles.Count; $index++) {

The same class’s Verbose line, Flushed batch N with <count> results, always said 0, because it counted the batch after clearing it.

One more fix in the same stretch: Invoke-MemoryCheck rated the .NET managed heap against -MaxMemoryUsageMB, a limit the memory manager compares with the process working set. The heap is a fraction of the working set, so the check could say Normal while the manager’s own warning fired. It rates the working set now and reports the heap beside it.