Skip to content

Powershell

cmd和ps设置代理

win11中powershell设置v2ray代理 | Kifroom

cmd

set all_proxy=socks5://127.0.0.1:10808
set all_proxy=

powershell

# 设置**临时**代理 
$env:http_proxy="http://127.0.0.1:7897"   $env:https_proxy="https://127.0.0.1:7897"   
$env:all_proxy="socks5://127.0.0.1:10808"   
# (**端口号为你代理软件socks5协议的端口**)  

# 删除当前临时代理  
$env:all_proxy=""

# 查看当前环境变量  
ls env:*

oh-my-posh

推荐使用powershell

安装字体如果终端里下载失败,可以手动去github上下载再安装

设置主题:notepad $PROFILE 打开配置文件

oh-my-posh init pwsh | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_lean.omp.json" | Invoke-Expression

. $PROFILE

如果报错。由于您 PowerShell 环境中安装的 PSReadLine 模块版本过低,与 oh-my-posh 的初始化脚本不兼容所导致的。在开始菜单右键点击 "Windows PowerShell",然后选择“以管理员身份运行”。安装

Install-Module -Name PSReadLine -Force -AllowClobber

或更新

Update-Module -Name PSReadLine -Force

PowerShell 启动慢问题排查与解决记录

问题现象

PowerShell 因加载 condaoh-my-posh 导致启动缓慢。

诊断

  1. 隔离变量:使用 pwsh -NoProfile 启动。若速度正常,则问题在配置文件。

  2. 列出所有配置文件

    PowerShell

    $PROFILE | Get-Member -MemberType NoteProperty
    
  3. 定位脚本

    • conda 初始化脚本通常位于 $PROFILE.CurrentUserAllHosts (~\Documents\PowerShell\profile.ps1)。
    • oh-my-posh 配置位于 $PROFILE.CurrentUserCurrentHost (~\Documents\PowerShell\Microsoft.PowerShell_profile.ps1)。

解决方案

1. oh-my-posh 配置

问题:在 $PROFILE.CurrentUserCurrentHost 中存在重复的 oh-my-posh init 命令。

处理前:

PowerShell

oh-my-posh init pwsh | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_lean.omp.json" | Invoke-Expression

处理后 (保留一个):

PowerShell

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_lean.omp.json" | Invoke-Expression

2. conda 配置

问题:在 $PROFILE.CurrentUserAllHosts 中自动执行的 conda shell.powershell hook 耗时过长。

处理方案: 将自动加载改为按需加载。

步骤一:禁用自动加载

编辑 $PROFILE.CurrentUserAllHosts,注释掉 conda initialize 代码块。

处理前:

PowerShell

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "E:\softwares\miniconda3\Scripts\conda.exe") {
    (& "E:\softwares\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

处理后 (添加 # 注释):

PowerShell

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
# If (Test-Path "E:\softwares\miniconda3\Scripts\conda.exe") {
#     (& "E:\softwares\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
# }
#endregion

步骤二:创建按需加载函数

编辑 $PROFILE.CurrentUserCurrentHost (notepad $PROFILE),添加以下函数。

PowerShell

# 手动初始化 Conda 的函数
function Start-Conda {
    # 1. 初始化 Conda
    #    修改为你的 conda.exe 实际路径
    $condaPath = "E:\softwares\miniconda3\Scripts\conda.exe"
    if (Test-Path $condaPath) {
        & $condaPath "shell.powershell" "hook" | Out-String | Invoke-Expression
    }

    # 2. 刷新 oh-my-posh 主题以正确显示 Conda 环境
    #    注意:此行应与你配置文件中的 oh-my-posh 命令保持一致
    oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_lean.omp.json" | Invoke-Expression
}

使用

PowerShell 启动后,执行 Start-Conda 以加载 Conda 环境并刷新提示符。

Comments