Administrator
Published on 2025-04-24 / 4 Visits
0
0

配置 PowerShell 7

在 Windows 10 中安装 PowerShell 的步骤如下:


1. 确认现有版本

Windows 10 默认已集成 Windows PowerShell 5.1。按以下步骤检查:

  1. Win + S,输入 PowerShell
  2. 右键点击 Windows PowerShell,选择 以管理员身份运行
  3. 输入命令:
    $PSVersionTable.PSVersion
    
    • 若显示 5.1.x,则表示已安装默认版本。

2. 安装 PowerShell 7(最新版本)

若需升级到跨平台的 PowerShell 7(推荐),选择以下任一方法:

方法 1:通过 Microsoft Store 安装

  1. 打开 Microsoft Store
  2. 搜索 PowerShell
  3. 选择 PowerShell 7,点击 获取 安装。

方法 2:手动下载安装包

  1. 访问 PowerShell GitHub 发布页
  2. 下载 .msi 安装包(选择对应系统架构,如 x64)。
  3. 双击运行安装包,按向导完成安装。

方法 3:使用 winget(包管理器)

  1. 打开 CMDPowerShell
  2. 输入命令:
    winget install --id Microsoft.PowerShell
    

3. 验证安装

  1. 安装完成后,按 Win + S 搜索 PowerShell 7
  2. 运行后输入:
    $PSVersionTable.PSVersion
    
    • 显示 7.x.x 即表示成功。

4. 设置 PowerShell 7 为默认

  • 临时切换:在 CMD 中输入 pwsh 启动 PowerShell 7。
  • 修改默认终端(Windows 11/10 新版):
    1. 右键任务栏 → 设置隐私和安全性开发者选项
    2. 终端 中选择 Windows 终端 并设置为默认。

常见问题

  • 权限问题:以管理员身份运行安装程序。
  • 兼容性:PowerShell 7 与 Windows PowerShell 5.1 共存,旧脚本仍可用 powershell.exe 运行。
  • 路径配置:安装后路径自动添加,若未生效可重启系统。


🎨 1. 安装美化终端(推荐 Windows Terminal)

PowerShell 7 搭配 Windows Terminal 是最佳组合:

  • 支持标签页
  • 支持自定义配色和字体
  • 支持 GPU 加速渲染

🧠 2. 使用 Oh-My-Posh 美化提示符

安装 Oh-My-Posh

winget install JanDeDobbeleer.OhMyPosh -s winget

设置配置文件($PROFILE):

打开 PowerShell 配置文件(如果不存在会创建):

notepad $PROFILE

在其中加入以下内容:

oh-my-posh init pwsh --config "$(oh-my-posh get shell-path pwsh)" | Invoke-Expression

你可以换成自己喜欢的主题,例如:

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

查看并选择主题:

Get-PoshThemes

⛩️ 3. 设置 Nerd Font 字体

Oh-My-Posh 使用特殊图标,需要 Nerd Fonts 才能正确显示:

  • 下载字体:https://www.nerdfonts.com/
  • 推荐字体:FiraCode Nerd Font、Cascadia Code Nerd Font、JetBrains Mono Nerd Font
  • 安装后在 Windows Terminal 中设置为默认字体

🔧 4. 安装 PSReadLine 插件增强输入体验

Install-Module PSReadLine -Force -SkipPublisherCheck

启用自动补全和语法高亮:

Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -Colors @{
    "Command" = [ConsoleColor]::Green
    "Parameter" = [ConsoleColor]::Cyan
    "String" = [ConsoleColor]::Yellow
}

🌟 5. 使用额外模块提升体验

模块功能说明
posh-gitGit 状态集成提示
z快速目录跳转 (z location)
Terminal-Icons给文件列表加图标

安装方法:

Install-Module posh-git -Scope CurrentUser -Force
Install-Module z -Scope CurrentUser -Force
Install-Module Terminal-Icons -Scope CurrentUser -Force

配置文件中添加:

Import-Module posh-git
Import-Module z
Import-Module Terminal-Icons

好的!下面是一个适用于 PowerShell 7 的 $PROFILE 示例配置文件,整合了:

  • Oh-My-Posh 提示符美化
  • posh-git Git 集成
  • z 快速跳转命令
  • Terminal-Icons 文件图标
  • PSReadLine 自动补全和语法高亮

✅ 示例 $PROFILE 配置内容:

# === Oh My Posh ===
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression

# === Terminal Icons ===
Import-Module Terminal-Icons

# === posh-git ===
Import-Module posh-git

# === z (快速目录跳转) ===
Import-Module z

# === PSReadLine 设置 ===
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -Colors @{
    "Command" = [ConsoleColor]::Green
    "Parameter" = [ConsoleColor]::Cyan
    "String" = [ConsoleColor]::Yellow
    "Operator" = [ConsoleColor]::Magenta
    "Variable" = [ConsoleColor]::White
}

# === 设置默认位置为用户目录 ===
Set-Location ~

🧱 前置安装模块(只需一次):

# 安装 Oh My Posh
winget install JanDeDobbeleer.OhMyPosh -s winget

# 安装所需模块
Install-Module PSReadLine -Scope CurrentUser -Force
Install-Module posh-git -Scope CurrentUser -Force
Install-Module z -Scope CurrentUser -Force
Install-Module Terminal-Icons -Scope CurrentUser -Force

🔤 字体推荐(Nerd Font):

请一定搭配 Windows Terminal 使用,并设置以下字体之一:

  • Fira Code Nerd Font
  • JetBrains Mono Nerd Font
  • Cascadia Code PL

下载地址:https://www.nerdfonts.com/font-downloads


🎨 推荐主题(可替换配置文件中的 theme):

你可以尝试:

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

其他主题也可以通过 Get-PoshThemes 预览,再用路径替换上面的 config


不过不用担心,我们可以走 手动安装 Oh-My-Posh 的路线,照样能美化 PowerShell 7,非常稳定。


🛠️ 手动安装 Oh-My-Posh 教程(适用于 Win10 1809)

✅ 第一步:下载 Oh-My-Posh

  1. 打开这个链接:
    🔗 https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest

  2. 找到类似 Assets 区域,下载这个文件(适用于大多数人):

    oh-my-posh-windows-amd64.exe
    
  3. 下载后,把它重命名为:

    oh-my-posh.exe
    
  4. 移动到你常用的路径,比如:

    C:\Program Files\oh-my-posh\
    
  5. 把这个路径加进系统的环境变量 Path

    • 控制面板 → 系统 → 高级系统设置 → 环境变量 → 系统变量 → 找到 Path → 编辑 → 添加 C:\Program Files\oh-my-posh\
  6. 打开 PowerShell,验证:

    oh-my-posh --version
    

✅ 第二步:安装主题文件(必需)

  1. 同样在 GitHub Releases 页面中,找到:

    themes.zip
    
  2. 下载并解压到一个固定位置,比如:

    C:\Program Files\oh-my-posh\themes\
    

✅ 第三步:编辑 PowerShell 配置文件 $PROFILE

notepad $PROFILE

添加以下内容(确保路径根据你解压的位置调整):

# 初始化 Oh My Posh(使用 Paradox 主题为例)
oh-my-posh init pwsh --config "C:\Program Files\oh-my-posh\themes\paradox.omp.json" | Invoke-Expression

# 其他插件(可选)
Import-Module Terminal-Icons
Import-Module posh-git
Import-Module z

# PSReadLine 设置
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -Colors @{
    "Command" = [ConsoleColor]::Green
    "Parameter" = [ConsoleColor]::Cyan
    "String" = [ConsoleColor]::Yellow
}

✅ 第四步:安装 PowerShell 模块(仍然可以用 PowerShell 自己的包管理器)

Install-Module PSReadLine -Scope CurrentUser -Force
Install-Module posh-git -Scope CurrentUser -Force
Install-Module z -Scope CurrentUser -Force
Install-Module Terminal-Icons -Scope CurrentUser -Force

✅ 第五步:设置 Nerd Font(必做)

从这里下载字体并安装:
📎 https://www.nerdfonts.com/font-downloads

建议下载:

  • JetBrainsMono Nerd Font
  • FiraCode Nerd Font

安装后到 Windows TerminalPowerShell 属性 中手动设置该字体。



Comment