PowerShell Tricks - Trick 3: Better Prompt!

Posted in Powershell on July 16th, 2007 by nazeeh

This one is a small one really, but I found it to be quite useful. One of the things that irritated me was when I would navigate to a folder that is deeply nested under other folders. The prompt would get so damn long that it’s eating up the majority of my command line. So I wrote this small prompt function that handles long folders nicely:

function prompt 
{     
    $pwd = (get-location).path;
    
    if ($pwd.Length -gt 40)
    {
        $pwd = “…” + $pwd.substring($pwd.Length - 20, 20);
    }
        
    if ($IsAdmin)
    {
        Write-Host (“PS (Admin) “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Admin Shell: “ + $(get-location)
    }
    else
    {
        Write-Host (“PS “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Windows Powershell: “ + $(get-location)
    }
    return ” “
}

What this does is it checks if the current path is longer than 40 characters, if it is, it truncates it to the last 20 characters preceded by “…”.  

So as you can see, the path is nice and short and will never be larger than this size. The window title displays the full path so that you can tell with a glance exactly where you are.

Have fun!

PowerShell Tricks - Trick 2

Posted in Powershell on May 4th, 2007 by nazeeh

So this one is quite a handy one. Do you ever find yourself wanting to jump back and forth between folders? I have quite a few folders on my system (or on the network) that I use everyday. I wanted to have a way to quickly jump to each of these folders regardless where I am. At the same time, I wanted someway to be able to create such jump points whenever I want to.

The solution I came up with was to create functions that would change my location to the target folder I want to jump to. So I had a function I called “source” that would run “cd c:\source\” for instance. Then I took it one step ahead and created a function called “aliasthis” that would create such “jump” functions automatically.

To do that, I created a script file I called “aliases.ps1″. In it I added the following function:

   1: function aliasthis($alias)
   2: {
   3:     $path = (Get-Location).path;
   4:     if ($path.substring(1,1) -eq ‘:’)
   5:     {
   6:         $drive = $path.substring(0,2);
   7:         $script = “$drive ; cd `”$path`“”;
   8:     }
   9:     else
  10:     {
  11:         $script = “cd `”$path`“”
  12:     }
  13:
  14:     $function = “function global:$alias { $script }”;
  15:     add-content $env:psconfig\\myscripts\\aliases.ps1 $function
  16:     new-item function:global:$alias -value “$script”;
  17: }

To use that function, you type “aliasthis <aliasname>” and it will create a function with the name you specified that will take you to the location you ran the function from. So if you run it like this:

PS:c:\source> aliasthis source

It will create a function called “source” that will always take you to c:\source\.
The function does two things actually, it creates the function you want in the current shell session so you can use it right away, then

it adds the text of that function to the end of the aliases.ps1 file so that next time you start the shell, the function is available. So at the end of my aliases.ps1 file, I have stuff like:

function global:desktop { C: ; cd “C:Users\nazeehe\Desktop” }

Last, I make a call to aliases.ps1 from my profile.ps1 script to make sure this file gets loaded with every instance of the shell:
. aliases.ps1

That’s it! Works like a charm. I have quite a bit of jump functions declared now and I can make my way to any place on my PC or the network with just a few keystrokes!

PowerShell Tricks - Trick 1

Posted in Powershell on May 3rd, 2007 by nazeeh

Man I love Powershell! Finally there’s a shell on Windows that actually kicks major ass! I decided to start a series of Powershell tricks that I’ve learned. This one here is the first one :)

This first one is a simple piece of code in my profile.ps1 file that detects whether the shell is running as admin or not. If the shell is running as admin, it gets a red background to make that very clear:

To do that, edit your profile.ps1 file (or create it in a folder called WindowsPowershell in your Documents folder). The code I use to detect admin user or not is:

$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin)
{
  (get-host).UI.RawUI.Backgroundcolor=“DarkRed”
  clear-host
}


This will cause the shell to have a DarkRed background if you’re admin, otherwise it will be the default blue color.

The $IsAdmin variable is then defined throughout the script, so you can use it for any other customization such as the prompt:

function prompt
{
    if ($IsAdmin)
    {
        Write-Host (“PS (Admin) “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Admin Shell: “ + $(get-location)
    }
    else
    {
        Write-Host (“PS “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Windows Powershell: “ + $(get-location)
    }
    return ” “
}

That’s it! Have fun! More stuff to come soon.

The page's WebCounter count says that you are visitor number Visitor Counter by Digits
FireStats icon Powered by FireStats