【发布时间】:2022-01-25 18:31:41
【问题描述】:
使用 Windows PowerShell,如何更改命令提示符?
例如,默认提示说
PS C:\Documents and Settings\govendes\My Documents>
我想自定义那个字符串。
【问题讨论】:
标签: windows powershell
使用 Windows PowerShell,如何更改命令提示符?
例如,默认提示说
PS C:\Documents and Settings\govendes\My Documents>
我想自定义那个字符串。
【问题讨论】:
标签: windows powershell
只需将函数 prompt 放入您的 PowerShell 配置文件 (notepad $PROFILE),例如:
function prompt {"PS: $(get-date)>"}
或彩色:
function prompt
{
Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White
return " "
}
【讨论】:
notepad $PROFILE 在管理员 powershell 提示符下无法在 Windows 7 中工作
new-item -itemtype file -path $profile -force
Set-ExecutionPolicy RemoteSigned。
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted 如果您只想为当前用户更改或无法以管理员身份运行。
与Ocaso Protal's answer 的评论相关,Windows Server 2012 和 Windows 7(在 PowerShell 窗口中)需要以下内容:
new-item -itemtype file -path $profile -force
notepad $PROFILE
如果您使用多个用户名(例如您自己 + 生产登录名)运行,我会建议以下提示:
function Global:prompt {"PS [$Env:username]$PWD`n>"}
(这要归功于 David I. McIntosh。)
【讨论】:
Set-ExecutionPolicy RemoteSigned。
如果您想自己做,那么Ocaso Protal's answer 是您的最佳选择。但如果你像我一样懒惰,只想为你做点什么,那么我强烈推荐Luke Sampson's Pshazz package。
只是为了让你看看你有多懒,我将提供一个快速教程。
scoop install pshazz) 安装 Pshazzpshazz use msys)Pshazz 还允许您创建自己的主题,这与配置 JSON 文件一样简单。 Check out mine 看看有多简单!
【讨论】:
在提示符下,我喜欢网络驱动器的当前时间戳和已解析的驱动器号。为了使其更具可读性,我把它分成两行,并在颜色上玩了一下。
使用 CMD,我最终得到了
PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G
对于 PowerShell,我得到了相同的结果:
function prompt {
$dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss"
$currentDirectory = $(Get-Location)
$UncRoot = $currentDirectory.Drive.DisplayRoot
write-host "$dateTime" -NoNewline -ForegroundColor White
write-host " $UncRoot" -ForegroundColor Gray
# Convert-Path needed for pure UNC-locations
write-host "PS $(Convert-Path $currentDirectory)>" -NoNewline -ForegroundColor Yellow
return " "
}
这更具可读性:-)
顺便说一句:
powershell_ise.exe $PROFILE 而不是(愚蠢的)Notepad。【讨论】:
如果您 Set-Location 到网络共享,此版本的 Warren Stevens' answer 可避免路径中出现嘈杂的“Microsoft.PowerShell.Core\FileSystem”。
function prompt {"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> "}
【讨论】:
只显示我使用的驱动器号:
function prompt {(get-location).drive.name+"\...>"}
然后恢复到我使用的路径:
function prompt {"$pwd>"}
【讨论】: