Customizing the PowerShell console prompt style method _powershell

Source: Internet
Author: User

The default command-line prompt for the PowerShell console is ps+ (current path) + arrow, shaped like this:

Copy Code code as follows:
PS C:\Users\ non-moss >

The front plus PS is a sign of PowerShell to distinguish it from the traditional cmd console. The current path and arrows are followed in order to maintain consistency with the General Command line. This design is understandable, but if I'm blogging, I need to execute a command and copy it together with the results. For example, I'm going to demonstrate get-date, open the console, and typing the command:

Copy Code code as follows:
PS C:\Users\ Non-moss > Get-date

April 16, 2014 14:48:47


The results are correct, but they look bad, and the path is superfluous, and it distracts the reader's attention. It is said that you can locate in the C packing directory:
Copy Code code as follows:

PS c:\> Get-date

April 16, 2014 14:52:56


This weakened the path, much better, but still uncomfortable. "C:\" or superfluous, I am not a bit of suspected obsessive-compulsive disorder Ah, if changed to this?
Copy Code code as follows:

Ps> get-date

April 16, 2014 14:57:07


This is good, can embody PowerShell style, and no redundant information. It can even continue to simplify and change to:
Copy Code code as follows:

>get-date

April 16, 2014 14:59:21

Get-date

April 16, 2014 14:59:48


The key to this discussion is actually the console hint style, and here's how to customize it.

Knowledge points

The console's hint style is controlled by the Automation function prompt. The definition of a function defaults to:

Copy Code code as follows:
"PS $ ($executionContext. SessionState.Path.CurrentLocation) $ (' > ' * ($nestedPromptLevel + 1))"
# . Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# . Externalhelp System.management.automation.dll-help.xml

All we have to do is overwrite this function.

1. The best change should be: Prompt support for a variety of styles, you can switch back and forth. Now that you want to switch, you have to have a variable to store the state. This variable is best global and is read-only.
2. Changes to PowerShell built-in automation functions and automation variables are valid only in the current console session, so it is a good idea to store the changes in profile profiles.

Effect Chart:

Implementation scripts:

Copy Code code as follows:

Function Prompt ()
{
Switch ($PSPromptMode)
{
' CMD '
{
"$ ($executionContext. SessionState.Path.CurrentLocation) $ (' > ' * ($nestedPromptLevel + 1))"
}

' Arrow '
{
' > '
}

' None '
{
' '
}
' Simple '
{
' Ps> '
}
Default
{
"PS $ ($executionContext. SessionState.Path.CurrentLocation) $ (' > ' * ($nestedPromptLevel + 1))"
}
}
}

<#
. Synopsis
Set console hint style
. DESCRIPTION
Set console hint style, support five styles: Normal,cmd,arrow,simple,none
#>
Function set-prompt
{
Param
[Parameter (mandatory= $true)]
[Validateset (' Normal ', ' Cmd ', ' Arrow ', ' simple ', ' None ', IgnoreCase = $true)]
$Mode
)
$varPSPromptMode = (get-variable ' pspromptmode '-ea silentlycontinue)
#配置变量不存在则新建
if ($varPSPromptMode-eq $null)
{
New-variable-name ' Pspromptmode '-value $Mode-scope ' Global '
$varPSPromptMode = Get-variable-name ' Pspromptmode '
$varPSPromptMode. Description = ' Prompt function configuration variable '

#限制配置变量的取值集合
$varPSPromptModeAtt = New-object System.Management.Automation.ValidateSetAttribute (' Normal ', ' Cmd ', ' Arrow ', ' Simple ', ' None ')
$varPSPromptMode. Attributes.Add ($VARPSPROMPTMODEATT)

#限制配置变量为只读并且可以贯穿所有作用域ls
$varPSPromptMode. Options = ' ReadOnly, Allscope '

}
#更新配置
#只读变量可以通过-force option to update values
Set-variable-name Pspromptmode-value $Mode-force
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.