2011-04-20 127 views

回答

75

只要把函數prompt在PowerShell配置文件(notepad $PROFILE),例如:

function prompt {"PS: $(get-date)>"} 

或彩色:

function prompt 
{ 
    Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White 
    return " " 
} 
+0

'$記事本PROFILE'無法在Windows 7的管理員工作PowerShell提示符 – jcollum 2011-11-08 23:33:11

+16

啊,我看你需要首先創建配置文件:'新項目-itemtype文件-path $輪廓-force ' – jcollum 2011-11-08 23:40:00

+2

注意:您可以將提示功能粘貼到PowerShell中以更改提示路徑,而不是將該功能保存在配置文件中,但每次啓動PowerShell時都必須執行此操作。 – 2012-12-09 09:00:54

14

相關的上述評論,以下是Windows Server 2012以及Win7所需:

new-item -itemtype file -path $profile -force 
notepad $PROFILE 

如果您使用多個用戶名(例如,多個用戶名)運行,那麼我會提示以下內容作爲提示。自己+生產登錄):

function Global:prompt {"PS [$Env:username]$PWD`n>"} 

(信用大衛I.麥金託什這一個)

+0

您還需要以管理員身份運行Powershell並執行'Set-ExecutionPolicy RemoteSigned'。 – qed 2016-06-06 15:20:29

3

如果你想自己做,然後Ocaso Protal's answer是要走的路。但是如果你像我一樣懶惰,只想爲你做點什麼,那麼我強烈推薦Luke Sampson's Pshazz package

爲了向你展示你是多麼的懶惰,我將提供一個快速教程。

  • Scoopscoop install pshazz
  • 使用安裝Pshazz一個不錯的預定義的主題(pshazz use msys
  • 飲料(根)啤酒

Pshazz還允許您創建自己的主題,這是就像配置JSON文件一樣簡單。 Check out mine看看它是多麼容易!

0

在提示時,我喜歡當前時間戳併爲網絡驅動器解析驅動器號。爲了使它更具可讀性,我把它放在兩行中,並用顏色演奏了一下。

隨着CMD,我結束了

PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G 

對於PS,我得到了相同的結果:

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 " " 
} 

這是一個小更具有可讀性:-)

BTW:

  • 我更喜歡powershell_ise.exe $PROFILE而不是(愚蠢)記事本
  • 如果你喜歡用斷點調試你的prompt(),你應該將prompt-function重命名爲其他任何東西(或者在另一個文件中試試)。否則,您可能最終陷入循環:當您停止調試時,會再次調用prompt(),並再次停止在斷點處。很刺激,在第一...
0

這的Warren Stevens' answer版本避免了嘈雜「Microsoft.PowerShell.Core \ FileSystem」下的路徑,如果你Set-Location到網絡共享。

function prompt {"PS [$Env:[email protected]$Env:computername]$($PWD.ProviderPath)`n> "}