Skip to content

Instantly share code, notes, and snippets.

@luke-beep
Created December 1, 2023 19:05
Show Gist options
  • Save luke-beep/5c01a0ea9c77fd9142a020bab787d0d3 to your computer and use it in GitHub Desktop.
Save luke-beep/5c01a0ea9c77fd9142a020bab787d0d3 to your computer and use it in GitHub Desktop.
function Optimize-Assemblies {
param (
[string]$assemblyFilter = "Microsoft.PowerShell.",
[string]$activity = "Native Image Installation"
)
try {
$ngenPath = Join-Path ([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) "ngen.exe"
if (-Not (Test-Path $ngenPath)) {
Write-Host "Error: ngen.exe not found at $ngenPath. Ensure .NET Framework is installed."
return
}
$assemblies = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.FullName -ilike "$assemblyFilter*" }
if ($assemblies.Count -eq 0) {
Write-Host "No assemblies found matching filter: $assemblyFilter."
return
}
foreach ($assembly in $assemblies) {
$assemblyName = [System.IO.Path]::GetFileName($assembly.Location)
Write-Progress -Activity $activity -Status "Optimizing $assemblyName"
Start-Process -FilePath $ngenPath -ArgumentList "install `"$($assembly.Location)`"" -Wait -NoNewWindow
}
Write-Host "All specified assemblies have been optimized."
} catch {
Write-Host "An error occurred: $_"
}
}
Optimize-Assemblies -assemblyFilter "Microsoft.PowerShell."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment