zhendeshifeng 2020-06-22
内容并不是借助第三方脚本实现,是讲的如何DIY PowerShell


首先,更加美观的Windows Terminal
唐铭:Windows Terminal 终端入门?zhuanlan.zhihu.com
修改PowerShell的显示效果,是通过修改PowerShell启动时默认加载的一个脚本来实现的,也就是

因为PowerShell默认禁止运行脚本,需要首先开启权限,管理员模式运行PowerShell

然后输入命令,并输入Y确认
set-ExecutionPolicy RemoteSigned

接着
安装了visual stdio code并且添加了环境变量的可以直接在PowerShell中接着输入(推荐)
code $profile
没安装visual stdio code的普通用户则在PowerShell中输入
$profile

然后根据显示的路径去手动新建这个文件,最后用记事本打开

然后将下面的代码粘贴进配置文件中并ctrl+s保存
cls #清除微软广告
$path = $pwd.path
if ( $path.split("\")[-1] -eq "System32" ) {
# change default path to desktop
$desktop = "C:\Users\" + $env:UserName + "\Desktop\"
cd $desktop
}
Set-PSReadLineOption -Colors @{
Command = "#e5c07b"
Number = "#cdd4d4"
Member = "#e06c75"
Operator = "#e06c75"
Type = "#78b6e9"
Variable = "#78b6e9"
Parameter = "#e06c75" #命令行参数颜色
ContinuationPrompt = "#e06c75"
Default = "#cdd4d4"
Emphasis = "#e06c75"
#Error
Selection = "#cdd4d4"
Comment = "#cdd4d4"
Keyword = "#e06c75"
String = "#78b6e9"
}
function prompt
{
#Write-Host("$pwd>")
$path = $pwd.path
if ( -not $path.EndsWith("\") ) {
"" + $path.split("\")[-1] + " λ "
}
else {
"" + $path.split("\")[0] + " λ "
}
}其中,cls指令是为了清除PowerShell打开时的微软广告
如果打开时当前路径是System32这个系统文件夹,会切换到桌面
Set-PSReadLineOption为PowerShell命令的配色,参数具体含义以及如何修改参考微软官方文档
Set-PSReadLineOption (PSReadLine)?docs.microsoft.com
function prompt函数是PowerShell显示命令头部调用的函数,功能是显示下面画线部分,使用的PowerShell脚本语法编写

如果要输出特殊字符比如λ,需要将文件编码改成GB2312或者GBK,否则会乱码,

如果是希望实现类似于下图的git bash类似的效果,即多输出一行路径,用户或者时间等其他信息

直接取消掉我注释掉的Write-Host("$pwd"),然后将参数改为你理想的格式
图片中出现命令行翻译,看