Modifying the home in PowerShell

Link. January 24, 2008. Comments [0]. Posted in: PowerShell

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 $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\<user>\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.


Comments are closed.

About

Tomas Restrepo is co-founder of devdeo ltda. His interests include .NET, Connected Systems, PowerShell and, lately, dynamic programming languages. More...

email: tomas@winterdom.com
msn: tomasr@passport.com
twitter: tomas_restrepo

Technorati Profile

devdeo logo

View my profile on LinkedIn

MVP logo

Syndicate

Ads

Links

Tag Cloud

.NET (232) Architecture (47) ASP.NET (6) BizTalk (170) Blogging (64) C++ (3) Castle (2) Commerce Server (3) Development (118) DLR (7) Enterprise Services (25) Fonts (4) Host Integration Server (1) LINQ (3) Linux (5) NHibernate (1) Personal (143) PowerShell (22) QuickCounters (4) Tools (74) Vista (38) VS Color Scheme (10) VSTO (2) WCF (64) Web Services (87) WinFX (80) Workflow (47) WPF (5) XML (21)

Statistics

Total Posts: 986
This Year: 56
This Month: 6
This Week: 0
Comments: 755

Blogroll

Post Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 1.9.7174.0

Sign In