[玩转系统] 修饰你的提示
作者:精品下载站 日期:2024-12-14 07:43:00 浏览:12 分类:玩电脑
修饰你的提示
PS C:\> $function:prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
PS C:\>
这个提示来自 PowerShell v4,但我很确定它与 v3 中使用的函数相同。 PowerShell v2 有不同的功能。
PS C:\> $function:prompt
$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlev
el -ge 1) { '>>' }) + '> '
PS C:\>
您是否注意到新功能有一个帮助链接?尝试一下:
帮助提示-在线
您将获得 about_prompts 帮助主题的在线版本。提示功能的伟大之处在于你可以更改它。多年来我发布了各种提示。但这里还有 4 个供您尝试。这些提示应该在 v3 及更高版本中起作用。大多数功能都是对标准提示的简单添加,并且应该适用于控制台和 ISE。要尝试提示,您可以将该函数粘贴到 PowerShell 会话中。要使其“永久”,请将其插入到您的 PowerShell 配置文件脚本中。
包括 PowerShell 版本
Function Prompt {
"PS $($psversiontable.psversion.major) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}
此提示会将 PowerShell 主要版本插入到您的提示中。
包括管理员
Function Prompt {
$default = "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "[ADMIN] " -NoNewline -ForegroundColor Red
$default
}
else {
$default
}
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}
此提示将测试您是否以管理员身份运行,如果是,则会以红色文本插入 [ADMIN]。
包含计算机名
Function Prompt {
"[$($env:computername)] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}
您喜欢远程会话向您显示所连接的计算机的方式吗?为什么不一直这样呢?我所做的只是从 Computername 环境变量中插入本地计算机名。
自动导出命令历史记录
Function Prompt {
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
#define a log file with the host name, a time stamp and process ID
# WindowsPowerShellISEHost_20140715_7388.log
# the log file will go in the Windows PowerShell directory
$log = "{0}_{1:yyyyMMdd}_{2}.log" -f $host.name.Replace(" ",""),(get-date),$PID
$logfile = Join-Path -path $home\Documents\WindowsPowerShell -childpath $log
#only insert history if it is different than the last command ran
$mycmd = (Get-History -Count 1).CommandLine
if ($mycmd -ne (Get-Content -Path $logfile -Tail 1)) {
$mycmd | Out-File -FilePath $logfile -Encoding ascii -Append
}
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}
最后一个版本对转录进行了一些改动。当您运行脚本时,您会得到命令和结果。但也许您想要的只是记录您运行的所有命令。当然,您可以在会话结束时导出命令历史记录,但您必须记住这样做,如果超过最大历史记录计数,您将错过命令。在此提示中,每次按 Enter 时,它都会获取您运行的最后一个命令并将其附加到日志文件中。该日志文件在您的 PowerShell 目录中创建,并使用 PowerShell 主机的命名格式(不带空格)、时间戳 (年月日) 和当前 PowerShell 会话的进程 ID。这允许您使用单独的日志保留多个 PowerShell 会话。日志文件仅记录与您上次运行的命令不同的命令。这还允许您无需执行任何操作即可按 Enter 键,并且不会填充日志。
如果您临时粘贴了这些提示功能之一,但不喜欢它,您只需重新启动 PowerShell 即可获得原始提示。或者您可以使用此功能来恢复它。
Function Restore-Prompt {
#reset the original prompt or whatever you want to use as your default
Function global:Prompt {
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
} #original prompt
}
如果您正在尝试提示,可以很方便地将其放入您的 PowerShell 配置文件中。 Restore-Prompt 只是在全局范围内定义了一个新的 Prompt 函数。我使用的是默认的 PowerShell 提示符,但您可以将其更改为您想要的任何内容。
如果您在提示符下做了一些很酷的事情,我希望您能分享。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag