自定义PowerShell提示符 - 等同于CMD的$ M $ P $ _ $ + $ G? [英] Customizing PowerShell Prompt - Equivalent to CMD's $M$P$_$+$G?

查看:252
本文介绍了自定义PowerShell提示符 - 等同于CMD的$ M $ P $ _ $ + $ G?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用PowerShell玩弄,并试图让它表现。

I've started to "play around" with PowerShell and am trying to get it to "behave".

我想做的一件事是定制PROMPT以类似于在MS-Dos上执行的$ M $ P $ _ $ + $ G:

One of the things I'd like to do is to customize the PROMPT to be "similar" to what "$M$P$_$+$G" do on MS-Dos:

这些做:

字符 |描述

$ m 与当前驱动器号相关联的远程名称,如果当前驱动器不是网络驱动器,则为空字符串。

$ p 当前驱动器和路径

$ _ ENTER-LINEFEED

$ + 零个或多个加号(+)字符,取决于 pushd 目录堆栈的深度,每个级别推送一个字符

$ g >(大于号)

Character| Description
$m The remote name associated with the current drive letter or the empty string if current drive is not a network drive.
$p Current drive and path
$_ ENTER-LINEFEED
$+ Zero or more plus sign (+) characters depending upon the depth of the pushd directory stack, one character for each level pushed
$g > (greater-than sign)

因此最终输出结果如下:

So the final output is something like:

    \\spma1fp1\JARAVJ$ H:\temp
    ++>

我已经能够添加 $ M $ _ 功能(和一个漂亮的历史功能)到我的提示如下:

I've been able to add the $M and $_ functionality (and a nifty History feature) to my prompt as follows:

function prompt
{
   ## Get the history. Since the history may be either empty,
   ## a single item or an array, the @() syntax ensures
   ## that PowerShell treats it as an array
   $history = @(get-history)


   ## If there are any items in the history, find out the
   ## Id of the final one.
   ## PowerShell defaults the $lastId variable to '0' if this
   ## code doesn't execute.
   if($history.Count -gt 0)
   {
      $lastItem = $history[$history.Count - 1]
      $lastId = $lastItem.Id
   }

   ## The command that we're currently entering on the prompt
   ## will be next in the history. Because of that, we'll
   ## take the last history Id and add one to it.
   $nextCommand = $lastId + 1

   ## Get the current location
   $currentDirectory = get-location

   ## Set the Windows Title to  the current location
   $host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory

   ## And create a prompt that shows the command number,
   ## and current location
   "PS:$nextCommand $currentDirectory
>"
}

但其余的还不是我设法重复的...

But the rest is not yet something I've managed to duplicate....

非常感谢您的提示。

推荐答案

p>查看这是否符合您的需要:

See if this does what you want:

function prompt
{
   ## Get the history. Since the history may be either empty,
   ## a single item or an array, the @() syntax ensures
   ## that PowerShell treats it as an array
   $history = @(get-history)


   ## If there are any items in the history, find out the
   ## Id of the final one.
   ## PowerShell defaults the $lastId variable to '0' if this
   ## code doesn't execute.
   if($history.Count -gt 0)
   {
      $lastItem = $history[$history.Count - 1]
      $lastId = $lastItem.Id
   }

   ## The command that we're currently entering on the prompt
   ## will be next in the history. Because of that, we'll
   ## take the last history Id and add one to it.
   $nextCommand = $lastId + 1

   ## Get the current location
   $currentDirectory = get-location

   ## Set the Windows Title to  the current location
   $host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory

   ##pushd info
   $pushdCount = $(get-location -stack).count
   $pushPrompt = ""
   for ($i=0; $i -lt $pushdCount; $i++)
   {
       $pushPrompt += "+"
    }

   ## And create a prompt that shows the command number,
   ## and current location
   "PS:$nextCommand $currentDirectory `n$($pushPrompt)>"
}

这篇关于自定义PowerShell提示符 - 等同于CMD的$ M $ P $ _ $ + $ G?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆