Article directory
1. Problem description
powershell
Slow startup:
A configuration file that will be loaded for a long time every time it starts: Loading personal and system profiles took xxxx
ms
What I use is powershell 7.xx
that the 5.x that comes with the system may also have such a problem, for a reason.
The name of the new version is pwsh.exe, that is, enter win r, then enter pwsh
, and press Enter to open it quickly.
This is very useful, it is recommended to install (go to Microsoft official website to download and install).
2. Problem traceability
Since it took a long time to load the configuration file, let's see what the configuration file is.
enter:
$PROFILE
- $PROFILE.AllUsersAllHosts: Profiles valid for all users and all hosts.
- $PROFILE.AllUsersCurrentHost: Profile valid for all users and current host.
- $PROFILE.CurrentUserAllHosts: Profiles valid for the current user and all hosts.
- $PROFILE.CurrentUserCurrentHost: Profile valid for current user and current host. When you type $PROFILE directly, PowerShell actually returns the value of $PROFILE.CurrentUserCurrentHost, which is the profile in effect for the current user and current host.
will show the location of your configuration file, for example:
C:\Users\xxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Enter the relevant directory, you may find:
- no
Microsoft.PowerShell_profile.ps1
; - Only:
profile.ps1
;
After testing, the name does not affect.
Open the configuration file to see:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "E:\anaconda3\Scripts\conda.exe") {
(& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{
$_} | Invoke-Expression
}
#endregion
conda init powershell
This is about the content of the conda environment, which you generated after a certain execution:
Configuration file interpretation:
The purpose of this script is to check
conda.exe
whether exists in the specified path. If present, it executesconda.exe
to generate an init script for PowerShell and executes the script to set up Anaconda's environment. This ensures that when you use the command in PowerShellconda
, all necessary environment variables and settings are properly configured.
-
If (Test-Path "E:\anaconda3\Scripts\conda.exe") {
This is a conditional statement which checks
E:\anaconda3\Scripts\conda.exe
if the specified path (ie) exists.Test-Path
is a PowerShell command to determine if a file or directory exists. Ifconda.exe
exists in the specified path, then{}
the code inside the braces will be executed. -
(& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
This is a complex command, I'll explain it in parts:
-
& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook"
: This part&
is executed using the operatorconda.exe
. It passes two parameters toconda.exe
:shell.powershell
andhook
. This basically tellsconda
PowerShell to generate init scripts. -
| Out-String
: This partconda
converts the output of the command to string format.|
is a pipe symbol that passes the output of the previous command to the next command. -
?{$_}
: This is a filter which ensures that the string passed to the next command is not empty. -
Invoke-Expression
: This command executes the string passed to it as a PowerShell command. So,conda
the generated init script will be executed to set up Anaconda's environment.
-
-
}
This is
If
the closing brace of the statement.
3. Solutions
3.1 Test
Comment out the configuration file (# sign) and start powershell again.
Now it is opened in seconds, but it will not enter the conda environment (base).
3.2 Solution 1: Do not use conda in powershell
Using conda in powershell is nothing more than configuring a virtual environment. Can I use powershell?
Anaconda typically provides users with two PowerShell variants:
-
Anaconda PowerShell Prompt : This is a special PowerShell prompt that is pre-configured with all the necessary environment variables and settings so that users can use
conda
and other Anaconda tools directly. When you start from this prompt, you don't need to perform any additional initialization steps to use all of Anaconda's features. -
Anaconda Prompt (cmd) : This is a Windows command prompt (cmd.exe) based prompt that is also pre-configured with the Anaconda environment. While it's not PowerShell, it functions similarly to Anaconda PowerShell Prompt.
The purpose of both prompts is to simplify the user's workflow, allowing them to easily use Anaconda and its related tools without manually configuring the environment or performing additional initialization steps.
If you have Anaconda installed, you should be able to find these two prompts in the Windows Start menu. If you mainly use PowerShell, I recommend using " Anaconda PowerShell Prompt
", because it provides all the functions of PowerShell, and also integrates Anaconda's tools and settings (of course, the opening speed is relatively slow).
Therefore, in Solution 1, you can comment out the powershell configuration file and use Anaconda PowerShell Prompt.
3.2 Solution 2: Use conda in powershell when needed (recommended)
Still comment out the part about conda in the powershell configuration file.
Save the conda configuration as a separate file. When you need to use conda, just run this script in powershell.
specific methods:
- Run powershell to view the directory where the default command is executed, generally:
C:\Users\username
- For convenience, report the configuration file about conda to the above location, for example, name it: conda
.ps1
; - When you need to use conda in powershell, run this script:
./conda.ps1
(./
indicates this directory, same as Linux);
Four, powershell 7 features
PowerShell 7 introduces many new features and improvements. Here are some key features:
-
Cross-platform support : PowerShell 7 is cross-platform, supporting Windows, macOS, and Linux.
-
Compatible with Windows PowerShell : PowerShell 7 is designed to be compatible with Windows PowerShell 5.1, making migration even easier.
-
New Parallel feature : Pass
ForEach-Object -Parallel
, users can run script blocks in parallel. -
New Operators
??
: The and??=
operators are introduced for working with$null
values. -
Chaining operators
&&
: The and||
operators are introduced , allowing users to chain commands. -
Ternary Operator : PowerShell 7 added the ternary operator
a ? b : c
. -
New
Get-Error
command : This command provides a detailed, structured view of errors. -
Other Improvements : Includes performance improvements to JSON cmdlets, new
Get-SubString
commands, and many other enhancements and fixes. -
Command history: PowerShell 7 (and previous versions) has a command history feature. This feature allows the user to view and recall previously executed commands. (Different from cmd, even if it is closed and reopened, historical commands can also be queried) The following are some basic operations related to the historical command function:
-
Get-History : This command lists the history of all previously executed commands.
-
Invoke-History : This command allows the user to re-execute previous commands. For example,
Invoke-History -Id 5
the command with ID 5 will be re-executed. -
Add-History : This command allows the user to add commands to the history.
-
Clear-History : This command will clear all command history.
-
Up/Down Arrow Keys : In PowerShell, users can use the up/down arrow keys to recall and execute previous commands.
-
Ctrl + R : This is an interactive search feature that allows the user to search for previously executed commands.
-
-
Smart command completion: What I often use is: after entering a character, the history command will be displayed in light color, and it can be completed by right-clicking the keyboard
Write love you forever into the end of the poem ~