After a long time of procrastinating about it, I finally sat down and started putting together a simple profile script for PowerShell to make it a usable alternative for everyday tasks. It's an excellent shell, but without tweaking it a bit, it can be a bit painful to use at times.

One of the things I wanted to do was modify the global $HOME variable so that it points to my real home folder: one I've configured in the HOME environment variable and that bears no relationship to my user's windows profile directory.

I also wanted PowerShell to use this other folder when resolving filesystem paths starting with "~\", which can be very convenient. Here's what I came up:

#

# Set the $HOME variable for our use

# and make powershell recognize ~\ as $HOME

# in paths

#

set-variable -name HOME -value (resolve-path $env:Home) -force

(get-psprovider FileSystem).Home = $HOME

The first line sets the $HOME variable, and needs the -force switch to do it since it is a read-only variable. The second line simply changes the Home property of the ProviderInfo object for the FileSystem provider (I don't care about how the home gets interpreted in other provider contexts).

On a side note, I also don't keep my real profile script in the default locations used by PowerShell. Instead, I keep my profile script in a file called "_profile.ps1" in, you guessed it, my home folder. Then I simply dot-source it from one of the real profile scripts recognized by PowerShell (such as "c:\users\\Documents\WindowsPowerShell\profile.ps1"), like this:

. "$env:HOME\_profile.ps1"

Pretty cool stuff, and hopefully this will help me avoid procrastinating so much on making PowerShell my default shell on windows.

Technorati tags:


Tomas Restrepo

Software developer located in Colombia.