自定义powershell主题和字体

10次阅读

共计 5696 个字符,预计需要花费 15 分钟才能阅读完成。

主要还是为了方便以及好看吧,也借鉴了一些网上的资源, 然后自己折腾了一段时间搞这个 powershell 的配色主题,提高工作效率
下面是记录方法,也欢迎提问讨论;
配置步骤:

1)首先安装 Chocolatey,这个很重要,如果里觉得可以自己去 git 或别途径安装软件或字体,请跳过这一条, 安装 scoop 也可以;
安装可去官网下载,也可以代码联网安装,打开 powershell,推荐使用管理员打开;
然后输入以下代码安装:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-ObjectSystem.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

然后引入安装的包并添加 path 使命令行可调用

 @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

如有不懂自行百度,此处不多讲;

2)安装好之后
choco -v 可显示最新的版本号

这里我就不添加图片了
这里列出几个常用的 choco 命令

choco list -lo 列出所有的安装包
choco list --local-only 列出 choco 已安装的包

choco search software 查找包
choco upgrade software 升级包
choco uninstall software 卸载包

别的可使用 choco --help 查看需要使用的命令

这里我们通过安装 colortool 来改变默认的配色

choco install colortool

命令为
colortool -s 查看默认配色主题

colortool 主题名称 可以更变主题

后面其实设置好之后右键设置然后直接保存就好

3)接下来是字体的安装

字体可以去 github 上下载

这里我推荐
https://github.com/powerline/…
的 powerline 的字体

字体安装好之后可能不能在属性后直接设置,需要命令行,,这个下面会一起说

4)下载完字体后,接下来我这边是用的 oh-my-posh 模块

Install-Module posh-git
Install-Module oh-my-posh

安装好之后

5)引入模块并加载

Import-Module posh-git
Import-Module oh-my-posh

这个时候可以设置
set-theme 改变主题了

默认有好几个主题,

搭配上面提到的 colortool 可以搭配出很好看的主题

如果发现乱码等,,请再检查你的字体是否设置

如果发现没有安装的字体

接下来贴代码

Install-Module -Name PSReadLine -Force -SkipPublisherCheck
if (!(Test-Path -Path $PROFILE)) {New-Item -Type File -Path $PROFILE -Force}
@"
#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.PromptForegroundColor -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
    `$path = Get-FullPath -dir `$pwd
    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 "`$path" -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
    }
 
    # Writes the postfix to the prompt
    `$prompt += Write-Prompt -Object `$sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor `$lastColor
 
    `$timeStamp = Get-Date -UFormat %r
    `$timestamp = "[`$timeStamp]"
 
    `$prompt += Set-CursorForRightBlockWrite -textLength (`$timestamp.Length + 1)
    `$prompt += Write-Prompt `$timeStamp -ForegroundColor `$sl.Colors.PromptForegroundColor
 
    `$prompt += Set-Newline
 
    if (`$with) {`$prompt += Write-Prompt -Object "`$(`$with.ToUpper())" -BackgroundColor `$sl.Colors.WithBackgroundColor -ForegroundColor `$sl.Colors.WithForegroundColor
    }
    `$prompt += Write-Prompt -Object (`$sl.PromptSymbols.PromptIndicator) -ForegroundColor `$sl.Colors.PromptBackgroundColor
    `$prompt += ' '
    `$prompt
}
 
`$sl = `$global:ThemeSettings #local settings
`$sl.PromptSymbols.StartSymbol = ''
`$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F)
`$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
`$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
`$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
`$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
`$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
`$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
`$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
`$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
`$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
"@>$env:userprofile"\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Paradox.psm1"       # 建议自己把代码放到记事本然后替换路径,此条路径要改。。@"
chcp 88888
Set-PSReadlineOption -EditMode Emacs
function which(`$name) {Get-Command `$name | Select-Object Definition}
function rmrf(`$item) {Remove-Item `$item -Recurse -Force}
function mkfile(`$file) {"" | Out-File `$file -Encoding ASCII}
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox
"@ > $PROFILE
chcp 88888
Set-PSReadlineOption -EditMode Emacs
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

整体是把函数写进 powershell 的编辑里引入系统的字体
然后主题更新
最后就可以设置刚刚安装好的字体了

ps: 默认的主题可以通过 Set-Theme 主题 来更改

正文完
 0