编辑PowerShell自定义提示功能时遇到问题

时间:2020-06-22 15:58:40

标签: powershell

我正在尝试使用自定义的Prompt函数使PowerShell提示符更像Linux终端。

代码如下;

function Prompt(){
    $W = Split-Path -leaf -path (Get-Location)
    $prompt = Write-Prompt "$($env:UserName)@$($env:ComputerName):" -ForegroundColor Green
    $prompt += Write-Prompt $W -ForegroundColor DarkCyan
    $prompt += Write-Prompt '>'
    return ' '
}

问题将其添加到我的Powershell配置文件中只会给我>

https://i.stack.imgur.com/0NzYb.jpg

一个空格。我确实安装了豪华的git。任何帮助将不胜感激,

1 个答案:

答案 0 :(得分:0)

我不认为有一个名为“写提示”的命令

我有一段时间没有按提示玩了。要获取最后一个字符可能会有些棘手。无论如何,这里有两个选项只是使用不同的写入主机方法。

方法1,使用$ Host:

Function Prompt
{
    $W = Split-Path -Path (Get-Location).Path -Leaf
    $Host.UI.Write( "Green", $Host.UI.RawUI.BackGroundColor, "$($env:UserName)@$($env:ComputerName):" )
    $Host.UI.Write( "DarkCyan", $Host.UI.RawUI.BackGroundColor, $W )
    "> "
} # End Function Prompt

方法2,使用Write-Host:

Function Prompt
{
    $W = Split-Path -Path (Get-Location).Path -Leaf
    Write-Host -ForegroundColor Green "$($env:UserName)@$($env:ComputerName):" -NoNewline
    Write-Host -ForegroundColor DarkCyan "$w" -NoNewline
    "> "
} # End Function Prompt

让我知道这是否有帮助。