自定义PowerShell提示

自定义PowerShell提示,powershell,prompt,Powershell,Prompt,我正在寻找自定义Powershellprompt函数实现的不同示例。如果您有自己的自定义实现,请发布脚本。与现有资源的链接也很好 发布提示实际外观的屏幕截图(预览)的额外积分。这是我的提示功能 function prompt() { if ( Test-Wow64 ) { write-host -NoNewLine "Wow64 " } if ( Test-Admin ) { write-host -NoNewLine -f red "A

我正在寻找自定义Powershell
prompt
函数实现的不同示例。如果您有自己的自定义实现,请发布脚本。与现有资源的链接也很好


发布提示实际外观的屏幕截图(预览)的额外积分。

这是我的提示功能

function prompt() {
    if ( Test-Wow64 ) {
        write-host -NoNewLine "Wow64 "
    }
    if ( Test-Admin ) { 
        write-host -NoNewLine -f red "Admin "
    }
    write-host -NoNewLine -ForegroundColor Green $(get-location)
    foreach ( $entry in (get-location -stack)) {
        write-host -NoNewLine -ForegroundColor Red '+';
    }
    write-host -NoNewLine -ForegroundColor Green '>'
    ' '
}
我倾向于重新打字

function prompt { "PS> " }
每次我准备例子的时候,我都可以复制/粘贴给别人,特别是当我走的路很长,这只会分散我的注意力


我仍然计划编写一个合适的提示函数,通过使用当前目录(没有指向该目录的路径)或(如果是数字)下一个更高级别,向我显示驱动器和位置的有用近似值。但这可能是我自己的文件系统所特有的。而且我从来没有被默认的提示符打扰过,以至于无法真正执行它:-)

这是jaykul提示符的修改版本。好处是

-有一个当前的历史id,因此您可以很容易地从历史中调用以前的项目(您知道该id) -这是一个小小的提醒-我将我的任务添加到提示符中,这样我就不会忘记它们(请参阅sshot)

以下是:

它使用以下辅助功能:

function shorten-path([string] $path) {
   $loc = $path.Replace($HOME, '~')
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}

我经常使用posh作为计算,所以我设置$ans变量。

PS>100
100
PS>$ans*9
900
PS>$ans*$ans

810000,这是我的。只是在每个命令上都有历史ID,所以我可以很容易地识别命令的ID。我还使用windowtitle为我提供当前工作目录,而不是将其显示在提示符中

106 >  cat function:\prompt

    $history = @(get-history)
    if($history.Count -gt 0)
{
        $lastItem = $history[$history.Count - 1]
        $lastId = $lastItem.Id
    }

    $nextCommand = $lastId + 1
    $Host.ui.rawui.windowtitle = "PS " + $(get-location)
    $myPrompt = "$nextCommand > "
    if ($NestedPromptLevel -gt 0) {$arrows = ">"*$NestedPromptLevel; $myPrompt = "PS-nested $arrows"}
    Write-Host ($myPrompt) -nonewline
    return " "
许多人忘记的一件事是在自定义提示中处理嵌套提示。请注意,我选中$nestedPromplevel并为每个嵌套级别添加一个箭头


Andy

写入主机-非WLINE-前红色(“+”*(获取位置-堆栈).Count)
?:-)@Johannes,这在提示中添加了一系列+,表示我知道推堆栈有多深,我刚刚取消了foreach,因为实际上您不需要每个项目,只需要项目的数量。@Johannes,啊,明白了。需要更多的咖啡将任务面包屑放在提示符上的想法非常有趣。我可以把这一点纳入我的提示中。。。看起来像是第一个写入主机上的输入错误。“换行符”应该是“-NoNewline”,如下所示:写主机“$($toPrompt)>”-ForegroundColor Green-NoNewline}lovely!我添加了当前目录tho。Get LastLine是什么样子的?出现错误:get-LastLine:术语“get-LastLine”无法识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。我喜欢此提示的格式,简短而简单。当您刚开始学习像me:D这样的PS时,它也是定制您自己的提示符的一个很好的起点
function shorten-path([string] $path) {
   $loc = $path.Replace($HOME, '~')
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}
106 >  cat function:\prompt

    $history = @(get-history)
    if($history.Count -gt 0)
{
        $lastItem = $history[$history.Count - 1]
        $lastId = $lastItem.Id
    }

    $nextCommand = $lastId + 1
    $Host.ui.rawui.windowtitle = "PS " + $(get-location)
    $myPrompt = "$nextCommand > "
    if ($NestedPromptLevel -gt 0) {$arrows = ">"*$NestedPromptLevel; $myPrompt = "PS-nested $arrows"}
    Write-Host ($myPrompt) -nonewline
    return " "