This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
OptimizePowerShellStartup.ps1
68 lines (50 loc) · 2.2 KB
/
OptimizePowerShellStartup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
###############################################################################################################
# Language : PowerShell 4.0
# Filename : OptimizePowerShellStartup.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Optimize PowerShell startup by reduce JIT compile time with ngen.exe
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Optimize PowerShell startup by reduce JIT compile time with "ngen.exe"
.DESCRIPTION
Optimize PowerShell startup by reduce JIT compile time with "ngen.exe".
Script requires administrative permissions.
.EXAMPLE
OptimizePowerShellStartup.ps1
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Script/OptimizePowerShellStartup.README.md
#>
Begin{
}
Process{
# Restart script/console as admin with parameters
if(-not([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$Arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
Start-Process PowerShell.exe -Verb RunAs -ArgumentList $Arguments
Break
}
Write-Host -Object "Start optimization..." -ForegroundColor Yellow
# Set ngen path
$ngen_path = Join-Path -Path $env:windir -ChildPath "Microsoft.NET"
if($env:PROCESSOR_ARCHITECTURE -eq "AMD64")
{
$ngen_path = Join-Path -Path $ngen_path -ChildPath "Framework64\ngen.exe"
}
else
{
$ngen_path = Join-Path -Path $ngen_path -ChildPath "Framework\ngen.exe"
}
# Find latest ngen.exe
$ngen_application_path = (Get-ChildItem -Path $ngen_path -Filter "ngen.exe" -Recurse | Where-Object {$_.Length -gt 0} | Select-Object -Last 1).Fullname
Set-Alias -Name ngen -Value $ngen_application_path
# Get assemblies and call ngen.exe
[System.AppDomain]::CurrentDomain.GetAssemblies() | foreach { ngen install $_.Location /nologo /verbose}
Write-Host -Object "Optimization finished!" -ForegroundColor Green
Write-Host -Object "Press any key to continue..."
[void]$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
End{
}