02 Mar 2009

I installed PowerShell a few month ago, but never really used it since then. Last week, I read some articles about it and decided to start using it to see what it was worth. Even if it will probably not change my life as a developer, I can say that so far it has been quite interesting.

I've been using the Unix/Linux shell for some years now, and from time to time I miss it under Windows. The cmd.exe prompt is really aging and has countless limitations. It get very hard to do some things that would be really easy with a Unix prompt. Enters PowerShell, a command line utility that leverages the .NET platform. But I digress, my point is not to teach what PowerShell is nor to write an article on how to use it, there are already tons of tutorials online that will fulfil that need better than I can possibly do.

When you install PowerShell, you get some "Windows PowerShell" content added to the start menu, and when you open it you get a nice prompt window that opens up, starting in your home directory (note that the size of the window is bigger, I resized it to fit):


Now let's say that you use Total Commander or any tool of that kind, or that you install PowerShell Prompt Here tool. Using one of these, you open a PowerShell prompt somewhere. Well, you are back with the default cmd.exe size window, which is too small for any nowadays screen. If you want to a nice window like the one you get when starting PowerShell from the start menu, you'll have to modify your PowerShell profile.

Modify Your Profile

When PowerShell loads, it reads a profile for that is located at $profile variable value. Simply type $profile in the PowerShell window to see where it is (or should be) located. If it does not exist, you can create it.

So, in order to get a nice window, you have to modify some properties of the  object

$a = (Get-Host).UI.RawUI 

$a.ForegroundColor = "Gray" 
$a.BackgroundColor = "Black" 

$a.WindowTitle = "Windows PowerShell"

$b = $a.BufferSize
$b.Width = 120
$b.Height = 3000
$a.BufferSize = $b

$b = $a.WindowSize
$b.Width = 120
$b.Height = 50
$a.WindowSize = $b

It's pretty straightforward.


The Prompt

I was not very satisfied with the prompt (showing the PWD, present working directory). In order to modify it, you simply have to define a function called "prompt" that returns a String. I found a nice prompt script there, but was not satisfied with it as it hides the drive. I modified it a bit in order to show the drive's letter no matter how long the PWD is.

function prompt 
	$m = 30 # maximum prompt length 
	$d = 3 #size of the drive prompt to keep 
	$str = $pwd.Path 

	if ($str.length -ge $m + $d) 
		# The prompt will begin with "...", 
		# end with ">", and in between contain 
 		# as many of the path characters as will fit, 
		# reading from the end of the path. 
		$str = $str.substring(0, 3) + "(...)" + $str.substring($str.length - ($m + $d) + 4) 

	"$str > "; 


Now, this is what I wanted, but using it was not satisfactory for some reason. So I figured out that what I wanted was a prompt showing the command number, and the window displaying the PWD in the title.

function prompt 
	$str = $pwd.Path 
	$nextId = (h -count 1).Id + 1; 

	$Host.Ui.Rawui.WindowTitle = $str + " - Windows PowerShell" 

	"PS (" + $nextId + ") > "; 

So here it is, the prompt exactly as I wanted it.

blog comments powered by Disqus