Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

PowerShell Prompt

Saturday, 5 November, 2011 @ 7:44 AM < Adam Boddington
Tags: PowerShell

Inspired by Josh Einstein's PowerShell prompt, I put together my own version. The idea is to hide the path in the PowerShell prompt, shorten it to the current directory name, or get back to the full path, quickly and easily.

The Prompt function is used by PowerShell to determine what to display for the prompt. Josh's version is pretty neat. When PowerShell calls it (without a parameter) the appropriate string is returned based on the value of a global variable. When the user calls it (with a switch) the global variable is set and no string is returned.

My prompt works exactly the same way, but is broken up into two scripts/functions to keep it simple. The first script, Set-Prompt.ps1, takes a string parameter and sets a global variable. I opted for a string over switches to avoid having to type -.

param (
    [ValidateSet("None", "Non", "No", "N", "Short", "Shor", "Sho", "Sh", "S", "Full", "Ful", "Fu", "F")]
    [string]$PathSize = "Full"
$global:promptPathSize = $PathSize[0]

Next the Prompt function reads the global variable and returns the right prompt. I keep this in another script and dot source it from my profile, but it could be added directly to the profile instead.

function Prompt {
    switch ($promptPathSize) {
        "N"     { "PS> " }
        "S"     { "PS $(Split-Path $pwd -Leaf)> " }
        default { "PS $pwd> " }

Finally an alias eliminates even more key strokes.

Set-Alias pr Set-Prompt

The prompt can now be modified like this.


There are 0 comments.


Leave a Comment

Please register or login to leave a comment.

Quick Random String

Mercurial Prompt

Quick Random String

Mercurial Prompt

browse with Pivot



Building Neno

Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Architecture (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM