Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

shell - How can I launch powershell.exe with the "default" colours from the PowerShell shortcut?

I'm attached to the nice blue colour of the PowerShell window when you launch it from one of the pre-installed shortcuts. However, if you manually launch powershell.exe, you don't get these colours, you get black/white :(

This is because the default settings are set on the shortcut (.lnk) file:

PowerShell shortcut colour settings

I've got a "PowerShell Prompt Here" entry in Explorer context menu, and I'd like it to launch PowerShell using the same nice colours as the usual shortcut; black sucks, and it's confusing to have different coloured windows (especially when I have some old-school command windows open frequently that are also black!).

I've found two problems with trying to set this so far:

  1. Setting the colour from within PowerShell seems to only allow certain values (ConsoleColor enum), none of which match the one on the default shortcut.
  2. Setting the colour within the PS Profile causes only text written afterwards to honour the new background colour. Adding "cls" causes a nasty flash of the original colour as it starts.

Is there any way to launch PowerShell from a command line (ie. that I can embed in the registry as an Explorer context menu item) that will use the same settings as the shortcut?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Edit your profile script (pointed to by $profile) and set the desired colors yourself:

# set regular console colors
[console]::backgroundcolor = "darkmagenta"
[console]::foregroundcolor = "darkyellow"

# set special colors

$p = $host.privatedata

$p.ErrorForegroundColor    = "Red"
$p.ErrorBackgroundColor    = "Black"
$p.WarningForegroundColor  = "Yellow"
$p.WarningBackgroundColor  = "Black"
$p.DebugForegroundColor    = "Yellow"
$p.DebugBackgroundColor    = "Black"
$p.VerboseForegroundColor  = "Yellow"
$p.VerboseBackgroundColor  = "Black"
$p.ProgressForegroundColor = "Yellow"
$p.ProgressBackgroundColor = "DarkCyan"

# clear screen
clear-host

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...