這是一張有關標題為 Methods to Accelerate PowerShell Profile Loading Speed 的圖片

Methods to Accelerate PowerShell Profile Loading Speed

Inspect antivirus settings to eliminate long profile loading times and enhance development efficiency.

Introduction

After installing PowerShell, we often add extra extensions such as:

  1. PSFzf: Provides fuzzy search functionality.
  2. zoxide: Enables quick navigation of the file system.
  3. posh-git: Offers Git command prompts and autocompletion.
  4. Oh My Posh: Enhances PowerShell prompt styles.
  5. And more…

These plugins can be integrated into PowerShell as per their installation methods.

However, they can cause slow loading times, making each PowerShell startup lengthy.

Loading time took about 2 seconds

This article will detail and document my process of solving this issue.

Identifying the Problem

First, in PowerShell, enter the following command to edit the user profile:

1
2
# If VS Code is not installed, use notepad.
code $PROFILE

Currently, the profile loads PSFzf and zoxide modules as shown below.

1
2
3
4
5
6
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }

Invoke-Expression (& { (zoxide init powershell | Out-String) })

function grep { $input | out-string -stream | select-string $args }

Opening PowerShell shows a load time of approximately 2000 milliseconds. Using pwsh --noProfile, you can temporarily bypass the profile loading to assess if it is causing the delay.

1
2
3
4
5
PowerShell 7.4.3
Loading personal and system profiles took 2074ms.

PS C:\Users\Wells> pwsh --noProfile
PS C:\Users\Wells>

You will notice the load time is faster, indicating that the profile loading is indeed the issue.

Referencing PowerShell’s official issue, others have experienced similar problems, which were resolved by disabling antivirus software.

Antivirus Impact

I use Bitdefender, which features a Scan scripts function. Disabling this function restored normal load times. During testing, turning off Bitdefender Shield also improved load times.

Solution

You can add trusted script locations to the exception list. Using the Everything tool, search for keywords like psfzf or zoxide to find paths such as:

1
2
c:\users\<user>\documents\powershell\modules
c:\users\<user>\appdata\local\microsoft\winget\packages\ajeetdsouza.zoxide_microsoft.winget.source_8wekyb3d8bbwe

Adding these paths to Bitdefender’s exception list should reduce load times to under 500 milliseconds when opening a new PowerShell window.

Add PowerShell module path to exclusion list

However, modules like posh-git have inherently slow load times, which are not improved even when antivirus software is disabled or when they are added to the exception list.

References

  1. Loading personal and system profiles took so very long · PowerShell/PowerShell
Theme Stack designed by Jimmy