相信大家一定在网上看到过有很多人的PowerShell Console界面有个自己的个性化,而不是默认的千篇一律,今天我也来把我的Console界面做了一个个性化,其实在很多年前就已经是这样,只是今天再次拿出来做了一些改动,首先看一张运行图,如下:
是不是挺有个性?我们可以注意到最上边的标题栏目,包含了我在个人配置文件夹中的一些设置,有如下信息:
- 欢迎字符: Welcome Anders Back
- PowerShell的版本号
- Console界面也支持的行数识别
- 当前的运行路径显示
- Console界面的运行时间
- 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 Green
Write-Host ("PS") -NoNewline -ForegroundColor Red
Write-Host "[$Line]>" -NoNewline -ForegroundColor Yellow
return " "
}
以上6个功能其实非常简单,但是其中3点我们要在这里简单说明下原理,3、行数识别以及5、运行时间,6、所占内存大小
关于行数识别,我们使用的是$host.UI.RawUI.CursorPosition.Y类型属性下的Y轴来判断的。而运行时间则是使用获取Conosole进程的开始时间与当前时间的差值所计算得出,最后的内存占用则依然是使用Console进程的WorkingSet属性计算得出。
是不是非常简单,有个性!:)