配置个性化PowerShell Console Profile

来源:互联网 发布:国家电网公司网络大学 编辑:程序博客网 时间:2024/04/30 23:18

相信大家一定在网上看到过有很多人的PowerShell Console界面有个自己的个性化,而不是默认的千篇一律,今天我也来把我的Console界面做了一个个性化,其实在很多年前就已经是这样,只是今天再次拿出来做了一些改动,首先看一张运行图,如下:

是不是挺有个性?我们可以注意到最上边的标题栏目,包含了我在个人配置文件夹中的一些设置,有如下信息:

  1. 欢迎字符: Welcome Anders Back
  2. PowerShell的版本号
  3. Console界面也支持的行数识别
  4. 当前的运行路径显示
  5. Console界面的运行时间
  6. Console界面所占用的内存大小

代码如下:

$global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()function prompt{$host.ui.rawui.WindowTitle = "Welcome Anders Back  ---  " + "PowerShell" + " " + $Host.Version + "  Line: " + $host.UI.RawUI.CursorPosition.Y `+ "  ---  Current Location: " + $(Get-Location) + "  ---  RuningTime: " + ((((Get-Date)-(Get-Process -id $pid).starttime) -as [string]) -split '\.')[0] `+ "  ---  Memory Utilization: " + $('{0:n2}' -f ([double](Get-Process -Id $pid).WorkingSet/1MB)) + "MB"$Line=$host.UI.RawUI.CursorPosition.Y    Write-Host ("[^_^]") -NoNewline -ForegroundColor GreenWrite-Host ("PS") -NoNewline -ForegroundColor RedWrite-Host "[$Line]>"  -NoNewline -ForegroundColor Yellow    return " "}

以上6个功能其实非常简单,但是其中3点我们要在这里简单说明下原理,3、行数识别以及5、运行时间,6、所占内存大小

关于行数识别,我们使用的是$host.UI.RawUI.CursorPosition.Y类型属性下的Y轴来判断的。而运行时间则是使用获取Conosole进程的开始时间与当前时间的差值所计算得出,最后的内存占用则依然是使用Console进程的WorkingSet属性计算得出。


是不是非常简单,有个性!:)

0 0