美化 PowerShell

2023-05-16

美化 PowerShell


UPDATE 2022.3.4: 本文使用的 oh-my-posh 基于 V2 版本,而更新且功能更强大的新版本已经发布,如需使用请参考其官方文档。


1. 准备工作

Step1. 下载并安装 PowerShell Core(pwsh),可在 Github 页面 进行下载。

PowerShell Core 相比传统 PowerShell 优势很多,此处推荐安装

Step2. 确保 Windows 版本在 v1903 之上(可以运行 winver 进行查看)

Windows10 在 1903 版本之后,带来了全新的 conpty,解决了原本 winpty 的大量 bug

Step3. 安装 VSCode 以及 PowerLine fonts

  • VSCode 下载地址
  • PowerLine Fonts 仓库地址
  • Nerd Fonts 仓库地址

2. 安装 PowerShell 模块

以管理员运行 PowerShell,并执行命令:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

(可选)安装预览版 PSReadLine:

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

3. 创建启动配置文件

执行命令:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE

编辑并保存如下内容:

chcp 65001
Import-Module posh-git 
Import-Module oh-my-posh 
Set-Theme Agnoster

4. 配置 Pwsh 字体和主题颜色

启动 pwsh,可从标题栏右键 > 属性中配置字体,建议使用 PowerLine 字体。

在 Microsoft/Terminal 仓库 中,可以下载到 ColorTool 工具,其可以应用 iTerm2-Color-Schemes 仓库 中的主题配色。

使用方法为:

./colortool -b $THEME_FILE

接下来在标题栏右键 > 属性,之后什么都不用做,直接点击确定,就可以把当前控制台和默认控制台的配色方案进行切换。


5. VSCode 内置终端

需要配置终端的 shell 和字体
字体和程序

另外,需要启用 Conpty
conpty


6. 自定义主题

定位到如下路径(oh-my-posh 版本号要视情况而定):
路径

创建的自定义主题存放在此处即可,例如我基于 Agnoster.psm1Fish.psm1 修改而成的 AgnosterPower.psm1:

#requires -Version 2 -Modules posh-git

function Write-Theme {

    param(
        [bool]
        $lastCommandFailed,
        [string]
        $with
    )

    $lastColor = $sl.Colors.PromptBackgroundColor

    $prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor

    #check the last command state and indicate if failed
    If ($lastCommandFailed) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    #check for elevated prompt
    If (Test-Administrator) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    $user = [System.Environment]::UserName
    $computer = [System.Environment]::MachineName
    if (Test-NotDefaultUser($user)) {
        $prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    if (Test-VirtualEnv) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.VirtualEnvBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    }
    else {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    }

    # Writes the drive portion
    $prompt += Write-Prompt -Object (Get-ShortPath -dir $pwd) -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    $prompt += Write-Prompt -Object ' ' -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor

    $status = Get-VCSStatus
    if ($status) {
        $themeInfo = Get-VcsInfo -status ($status)
        $lastColor = $themeInfo.BackgroundColor
        $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $lastColor
        $prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
    }

    if ($with) {
        $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor -BackgroundColor $sl.Colors.WithBackgroundColor
        $prompt += Write-Prompt -Object " $($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
        $lastColor = $sl.Colors.WithBackgroundColor
    }

    # Writes the postfix to the prompt
    $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
    $prompt += ' '
    $prompt
}

$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$sl.Colors.SessionInfoBackgroundColor = [ConsoleColor]::DarkGray
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
$sl.Colors.WithForegroundColor = [ConsoleColor]::White
$sl.Colors.WithBackgroundColor = [ConsoleColor]::DarkRed
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White

7. 最终效果

final


参考链接

  • PowerShell 美化指南
  • README - oh-my-posh
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

美化 PowerShell 的相关文章

  • 在结果中只显示一列?

    这是一个简单的问题 但如何在下面的代码中选择特定的列 我只想显示 时间 列 而不显示其他任何内容 我尝试输入 FORMAT TABLE TIME 但它只是多次填充 TIME 而没有实际显示时间 server event Get Conten
  • 如何在一行中输出多个变量

    我试图确定 CSV 中的用户是否处于活动状态 此外 我想知道它们是否是服务帐户 用户帐户或基于 OU 的计算机帐户 一切都在膨胀 直到我尝试输出它 输出分为几行 每个变量一行 我希望输出在一行上 中间有逗号 这样我完成后就会有一个 CSV
  • 如何使用 Get-ChildItem 仅获取目录?

    我正在使用 PowerShell 2 0 我想通过管道输出某个路径的所有子目录 以下命令输出所有文件和目录 但我不知道如何过滤掉文件 Get ChildItem c mypath Recurse 我尝试过使用 Attributes获取属性
  • PowerShell 与 MongoDB C# 驱动程序方法不兼容?

    由 C 泛型引起的最新 MongoDB 驱动程序的问题 Cannot find an overload for GetCollection and the argument count 1 我可能可以使用其他没有泛型的 GetCollect
  • 自动递增 EC2 实例名称

    我在 Stackoverflow 上看到很多问题 询问是否有办法自动递增实例名称 例如 foo1 foo2 fooN 我想看看是否有办法在 Powershell 中执行此操作 我正在使用 AutoLaunchConfiguration AS
  • 如何使用 R 或 PowerShell 从文本文件中提取数据?

    我有一个包含如下数据的文本文件 This is just text Username SOMETHI C Text Account DFAG Finish time 1 JAN 2011 00 31 58 91 Process ID 202
  • 是否可以要求 Powershell 在 Windows 终端而不是新窗口中启动进程

    如果我在 Windows 终端 PowerShell 选项卡中运行 start process Powershell Verb runas 它将创建一个新窗口 有没有办法在 Windows 终端中创建选项卡 您必须使用 Windows 终端
  • Powershell 添加的字符串类型的 ParameterizedProperty Chars 属性是什么?

    请注意 C gt Get Member MemberType eq ParameterizedProperty TypeName System String Name MemberType Definition Chars Paramete
  • Powershell XMLDocument保存为无BOM的UTF-8

    我构建了一个 System Xml XmlDocument 类型的 XML 对象 scheme gettype IsPublic IsSerial Name BaseType True False XmlDocument System Xm
  • 如何使用 PowerShell 查找 CPU 和 RAM 使用情况?

    我试图让 PowerShell 提供 RAM 和 CPU 使用情况 但我无法弄清楚要使用哪个 WMI 类 我的计算机有两个处理器 因此拥有这两个处理器的信息会很有用 您还可以使用 Get Counter cmdlet PowerShell
  • Powershell日期类型无法找到

    我正在尝试使用PowerShell连接virustotal API 代码来自virustotal网站 我得到 无法找到类型 System Security Cryptography ProtectedData 错误信息 代码如下 funct
  • 我的 Powershell GUI 界面在打开网格视图时不断调整大小

    我目前正在构建一个复制到剪贴板工具 其中显示存储在文件夹中的 txt 文件列表 并且我使用 out gridview 来允许用户更好地选择和过滤列表 我已附上图片以供参考 单击加载 out gridview 的按钮后如何停止调整大小 Too
  • launch.json 中不允许使用属性 env [VSCode]

    我所做的就是在 VS 中初始化一个模板 Azure Functions 项目 当我尝试通过设置运行配置环境变量时launch json VS直接警告我这是不 允许的 此外 即使当我尝试运行我的 ps1无论如何 对于 env 来说 它不起作用
  • 使用 UTF-8 编码的 Powershell 字符串变量

    我检查了许多与此相关的问题 但找不到解决我的问题的东西 基本上 我想将 UTF 8 编码的字符串存储在变量中 然后使用该字符串作为文件名 例如 我正在尝试下载 YouTube 视频 如果我们打印视频标题 则会显示非英文字符 ytd这是you
  • 在powershell中,使用export-csv cmdlet,我的整数被引号封装,知道为什么吗?

    所以我运行一个sql查询 通过管道传输到export csv 唯一的问题是所有值都用引号封装 包括整数 这对我来说不可能作为整数重新导入到SQL中 有什么想法吗 CSV 没有类型的概念 它是纯粹基于字符串的格式 PowerShell 只引用
  • 有没有办法在 MS Windows(Powershell 或 CMD)的 ripgrep 中转义引号?

    我想找一个字符串 Hello Hello 以双引号开头 在文本文件中使用ripgrep 通常 在 Bash 或 ZSH 中 这可以通过用反斜杠转义或用单引号括起来来实现 rg Hello rg Hello 然而 在 MS Windows P
  • 通过powershell运行ADB命令

    所以我尝试通过 powershell 脚本运行一些 ADB 命令 这是我正在尝试做的一个简单示例 adb shell echo in adb shell su root echo you are now root ls cd data da
  • 在 .Net 中保持 Powershell 运行空间打开

    我正在尝试从 VB Net 中运行一些 PowerShell 代码 如果您知道的话 C 编码器也可能会有所帮助 代码的第一部分 我需要使用密码连接到 na 控制器 并且需要保持连接打开 我还有其他命令需要通过单击按钮来运行 获取文件 显示文
  • Powershell v2 远程处理和委派

    我在两台机器上安装了 Powershell V2 并在两台机器上运行 Enable PsRemoting 两台机器都是Win 2003 R2 并且都加入了同一个活动目录域 我可以成功地远程运行命令 所以PS远程处理是在本地服务器和远程服务器
  • PowerShell:函数没有正确的返回值

    我编写了一个 powershell 脚本来比较两个文件夹的内容 Dir1 d TEMP Dir1 Dir2 d TEMP Dir2 function Test Diff Dir1 Dir2 fileList1 Get ChildItem D

随机推荐

  • MYSQL单行长度不能超过 65535

    报错 xff1a Row size too large The maximum row size for the used table type not counting BLOBs is 65535 mysql属于关系型 xff0c 行式
  • jdbcTemplate.batchUpdate在批量执行的时候,性能差没有效果,看看怎么解决的。

    我用的阿里druid数据库连接池 xff08 其实这个和连接池毛线关系没得 xff09 xff0c 创建jdbctemplate在执行insert 15000条数据时 xff0c 我发现还是30条 xff0c 20条 xff0c 35条这样
  • rabbit-mq 本地环境搭建

    一 xff1a 安装RabbitMQ需要先安装Erlang语言开发包 xff0c 直接下载地址 xff1a http erlang org download otp win64 18 3 exe 尽量安装时不选择C盘 xff0c 避免操作系
  • oracle小数点前面没有0,纠结解惑

    一1 天前台人找到我 xff0c 说我们安装的数据库有问题 xff0c 为什么小数点前面是0就不显示呢 xff0c 我去看了一下 xff0c command窗口要显示 SQL gt create table ml test num numb
  • 网监后台管理系统设计思路

    本次做的是网监系统saas服务平台的后台管理系统 xff0c 不涉及复杂功能逻辑 就是从菜单 模板 系 统 组织架构 角色 用户的设计思路 产品需求 xff1a 在各个省市网监系统的数量不断增长 xff0c 且系统逻辑和功能模块大致相同 x
  • 分类算法 c4.5 详解

    C4 5是一系列用在机器学习和数据挖掘的分类问题中的算法 它的目标是监督学习 xff1a 给定一个数据集 xff0c 其中的每一个元组都能用一组属性值来描述 xff0c 每一个元组属于一个互斥的类别中的某一类 C4 5的目标是通过学习 xf
  • 正则表达式匹配Html代码中图片路劲

    正则表达匹配图片路径 public static string GetHtmlImageUrlList string sHtmlText 定义正则表达式用来匹配 img 标签 Regex regImg 61 new Regex 64 34
  • (GPU版)Pytorch+pycharm+jupyter安装记录(截至23年3月14日)

    由于搞了一台旧主机 xff0c 主机上没有pytorch等软件程序 xff0c 所以重新装一遍 xff0c 顺便记录一下 xff01 一 安装显卡GPU的驱动程序 xff0c 搞定CUDA先 WIN 43 R打开命令行 xff0c 输入命令
  • Alibaba官方最新发布的这份Java学习导图+彩版手册,真不是吹的

    时间飞逝 xff0c 转眼间毕业七年多 xff0c 从事 Java 开发也六年了 我在想 xff0c 也是时候将自己的 Java 整理成一套体系 这一次的知识体系面试题涉及到 Java 知识部分 性能优化 微服务 并发编程 开源框架 分布式
  • linux下使用第三方商店安装应用

    安装 snap store 进行下载 xff0c 相当与第三方应用商店 xff0c 但是往往比某一个官方软件源里面的应用要丰富或更实用 到 snap docs 中选择你的 linux 版本进入安装文档 xff0c 根据指示一步一步安装即可
  • Centos7离线安装sqlserver2017

    Centos7离线安装sqlserver2017 根据操作系统版本选择下载匹配的sqlserver版本 可以在这里找一下https packages microsoft com config 我选择是先在一台有网的机器上下载好rpm安装包之
  • HC-05蓝牙模块配置

    目录 1 连接蓝牙模块a 蓝牙模块通过USB转TTL连接电脑b 打开串口助手 xff0c 波特率设置为38400c 检验是否连接成功 2 配置波特率3 修改密码4 设置主从模式5 设置蓝牙连接模式6 查询自身地址7 添加配对蓝牙地址8 测试
  • Windows沙盒技术调研

    转载自 xff1a 移动云开发者社区 一 Windows沙盒技术介绍 Windows沙盒提供了轻型桌面环境 xff0c 可安全地隔离运行应用程序 沙盒环境中Windows软件保持 34 沙盒 34 状态 xff0c 并独立于主机运行 沙盒是
  • OS + Linux Shell bash / sh / ksh / csh / tcsh / adb shell

    s Android adb shell ADB Android debug bridge Android手机实际是基于Linux系统的 Google提供的ADB工具包带有fastboot exe rar http dl iteye com
  • kali利用CVE_2019_0708(远程桌面代码执行漏洞)攻击win7

    一 漏洞说明 2019年5月15日微软发布安全补丁修复了CVE编号为CVE 2019 0708的Windows远程桌面服务 xff08 RDP xff09 远程代码执行漏洞 该漏洞在不需身份认证的情况下即可远程触发 危害与影响面极大 目前
  • 数据库系统原理1

    第一章 数据库管理技术发展的不同阶段形成不同的特点 数据描述经历了三个阶段对应于三个数据模型 第二章 数据库系统的生命周期 xff0c 书中可能和我们学习软工的时候有些出入 xff0c 其实就是不同时间有不同的理解 xff0c 横看成岭侧成
  • ssh详解

    SSH ssh secure shell protocol 22 tcp 安全的 具体的软件实现 xff1a OpenSSH ssh协议的开源实现 xff0c CentOS dropbear xff1a 另一个开源实现 SSH协议版本 v1
  • spring框架的简单配置步骤——小马同学@Tian

    spring框架配置步骤 1 导入jar包 本教程使用spring5 1 5 xff0c 在pom xml中进行导入依赖 Maven方式 xff1a span class token tag span class token tag spa
  • PSReadLine - Powershell 的强化工具

    PSReadLine Powershell 的强化工具 UPDATE 2022 3 4 根据其 Github README 的说明 xff0c If you are using Windows PowerShell on Windows 1
  • 美化 PowerShell

    美化 PowerShell UPDATE 2022 3 4 本文使用的 oh my posh 基于 V2 版本 xff0c 而更新且功能更强大的新版本已经发布 xff0c 如需使用请参考其官方文档 1 准备工作 Step1 下载并安装 Po