Windows PowerShell:更改命令提示符

| 使用Windows PowerShell,如何更改命令提示符? 例如,默认提示说
PS C:\\Documents and Settings\\govendes\\My Documents>
我想自定义该字符串。
已邀请:
只需将功能
prompt
放在PowerShell配置文件(
notepad $PROFILE
)中,例如:
function prompt {\"PS: $(get-date)>\"}
或有色:
function prompt
{
    Write-Host (\"PS \" + $(get-date) +\">\") -nonewline -foregroundcolor White
    return \" \"
}
与对Ocaso Protal的答案的评论相关,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所有。)
在提示符下,我喜欢当前的时间戳和已解析的网络驱动器的驱动器号。为了使其更具可读性,我将其分成两行,并使用了一些颜色。 有了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
而不是(哑)记事本。 如果您想使用断点调试提示符(),则应将提示符功能重命名为其他任何名称(或在另一个文件中尝试)。否则,您可能会陷入循环:停止调试时,会再次调用hint(),然后再次在断点处停止。起初很烦人...
如果您想自己做,那么Ocaso Protal的答案就是解决之道。但是,如果您像我一样懒惰,只是想为您做点什么,那么我强烈推荐Luke Sampson的Pshazz软件包。 为了向您展示您有多懒,我将提供一个快速教程。 使用Scoop(
scoop install pshazz
)安装Pshazz 使用一个不错的预定义主题(
pshazz use msys
) 喝啤酒 Pshazz还允许您创建自己的主题,就像配置JSON文件一样简单。看看我的,看看有多容易!
如果您“共享”网络共享,则此版本的沃伦·史蒂文斯(Warren Stevens)的答案避免了路径中嘈杂的“ Microsoft.PowerShell.Core \\ FileSystem \”。
function prompt {\"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> \"} 
仅显示驱动器号,我使用:
            function prompt {(get-location).drive.name+\"\\...>\"}
然后返回到我使用的路径:
            function prompt {\"$pwd>\"}

要回复问题请先登录注册