自定义powershell主题和字体

主要还是为了方便以及好看吧,也借鉴了一些网上的资源,然后自己折腾了一段时间搞这个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 查看需要使用的命令

自定义powershell主题和字体_第1张图片

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

choco install colortool

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

colortool 主题名称 可以更变主题
自定义powershell主题和字体_第2张图片

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

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 改变主题了

默认有好几个主题,
自定义powershell主题和字体_第3张图片
搭配上面提到的colortool可以搭配出很好看的主题

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

如果发现没有安装的字体

接下来贴代码

这个是在c:/program files/powershell/Microsoft.PowerShell_profile.ps1

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 65001   ##这个是c:/user/document/powershell/Microsoft.PowerShell_profile.ps1
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

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

自定义powershell主题和字体_第4张图片

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

你可能感兴趣的:(powershell)