Passion * Technology * Ruthless Competence

Monday, July 28, 2008

DevHawk's Slightly Useful Powershell Configuration

Since folks were interested in my favorite tools, I thought I'd blog how I have PowerShell configured. I'm not an ultra-power user, but I hold my own and hopefully you can use some of this configuration for yourself. Please tell me you're not still using CMD.

First, I use a trick I picked up from Tomas Restrepo to change your home directory and profile script. Here's my Microsoft.PowerShell_profile.ps1 file (in the WindowsPowerShell subdirectory of My Documents)

# reset $HOME and $PROFILE variables
set-variable -name HOME -value "D:\HPierson.Files" -force 
(get-psprovider FileSystem).Home = $HOME 
set-variable -name Profile -Value "$Home\Scripts\_profile.ps1"

# Run the $PROFILE script
. $profile

By default, PS uses the user's personal directory (c:\users\hpierson in my case) as the home directory and the aforementioned filename for the profile script. Personally, I like to keep all "real" data off my boot partition so that I don't have to back it all up when I repave. So my "real" home location is d:\HPierson.Files. The above script sets both the $HOME variable and file system home property to this directory. It also resets the $PROFILE variable to a script in my $home\Scripts folder and runs it.

My $PROFILE script does several things of note:

  • It adds the aforementioned $home\Scripts folder to the path. My utilities folder is a permanent part of the path, put I only add the scripts folder when I'm actually running PS.
  • If I'm running as administrator, I set the background color of the console window to red. I think I picked up this script from Brad Wilson at some point.
  • Set location to home, otherwise when I start PS as admin, it starts in c:\win\sys32.
  • I have a simple prompt script file that displays current folder, the current command number and a list of yellow plus signs indicating how deep I am in the directory stack. To get it to work, I have to remove the standard prompt function, which I do in $PROFILE.
  • I can't ever remember the space between "cd" and "..", so I wrote a simple function called "cd.." that executes "cd ..".
  • I have a su function that leverages the Script Elevation PowerToys. If you pass in a command, it executes it with elevated credentials. If you just execute su, it runs an elevated PowerShell.
  • I use 7-zip for my compression needs, including the 7za command line app. However, PS has issues w/ executing an exe that starts with a number. So I aliased 7za as "zip". UPDATE: Tomas points out that you can prepend an ampersand to force execution, so I could have typed "&7za". I forgot that when I created the alias and am now used to using zip, so I'm not going to change it. But I thought you should know.
  • I have an ever-changing set of aliases, depending on my needs. Currently, I alias "ipy", "cpy", "fsi", "fsc", "devenv" and "chiron" to their fully path-qualified equivalents, so I can run them from anywhere without having to add their respective folders to the path.

I don't set vsvars in the $profile script, but I do have a copy of the one Chris Tavares wrote in my scripts folder, so I can set up a VS environment in a moments' notice.

Also, I put PowerShell on the Vista quick launch bar, so I can bring it up by typing Win-2.

Posted By Harry Pierson at 9:52 AM Pacific Daylight Time
Monday, July 28, 2008 9:23:03 AM (Pacific Standard Time, UTC-08:00)
Any chance of pasting the actual $profile for us to work on?
Philip Craig
Monday, July 28, 2008 9:23:51 AM (Pacific Standard Time, UTC-08:00)
Would it be possible to post the actual $profile for us to see/copy/paste?
Philip Craig
Monday, July 28, 2008 11:20:16 AM (Pacific Standard Time, UTC-08:00)
yes, I am still using CMD! (Simple reason for it, not important here.)

But I have flagged this one as good things to do if and when I succumb to PowerShell. Also, these are all good things to do in my various SFA and SUA shells too, as I get more into those subsystems. (CMD and C Shell, I really am a throw-back!)
Wednesday, July 30, 2008 6:42:18 AM (Pacific Standard Time, UTC-08:00)
You should give the Powershell Community Extensions a look. They have functions that cover the CD.. and CD... as well as zip functions and many more.

http://www.codeplex.com/PowerShellCX/Wiki/View.aspx?title=PSCX%201.1.1%20Features&referringTitle=Home
Wednesday, July 30, 2008 10:29:48 AM (Pacific Standard Time, UTC-08:00)
@Richard, I keep meaning to take a look at the PS Community Extensions. Just haven't made the time yet. Thanks for the heads up.
DevHawk
Comments are closed.

SoCal Code Camp

PDC08

patterns & practices
Summit 2008

Change Congress
Recent Bookmarks
Tags .NET Framework (2) ADO.NET (5) Agile (7) AJAX (3) Architecture (284) Guidance (6) Interop (2) Modelling (61) Patterns (7) Process (4) SOA (93) Web Services (5) ASP.NET (24) Battlestar Galactica (3) BI (2) BizTalk (4) Blogging (115) dasBlog (11) Podcasting (4) BPM (1) C# (10) C++ (4) Capitals (5) CardSpace (3) CLR (2) College Football (10) Comedy Central (1) Community (81) Concurrency (6) Consumer Electronics (1) Database (13) Dependency Injection (2) Development (117) C Plus Plus (1) Embedded (5) Lanugages (37) Media (2) P2P (11) Rotor (1) SharePoint (6) SOP (3) DIY (1) DLR (16) Domain Specific Languages (13) Durable Messaging (5) Dynamic Languages (10) Dynamic Silverlight (1) Education (3) Enterprise 2.0 (1) Entertainment (14) ETech (15) F# (51) Functional Programming (17) Game Development (2) Guidance Automation (3) Hardware (8) HawkEye (3) Hockey (29) Home Electronics (1) Home Network (5) Humor (5) IASA (1) Idempotence (3) infrastructure (5) Instrumentation (4) Integration (2) IronPython (45) IronRuby (12) Java (2) Job (3) LINQ (23) Live Mesh (2) Lost (1) Master Data Management (1) Media 2.0 (6) Microsoft (30) MIX06 (2) Mobile Phone (1) Monads (5) Morning Coffee (172) Object Oriented (4) Office (5) Open Source (5) Open Space (2) Operations (3) Other (135) Art (1) Books (1) Family (31) Games (18) General Geekery (26) Home Theater (1) Movies (23) Music (20) Politics (3) Society (1) Sports (37) Working at MSFT (15) Parallel Programming (3) Parsing Expression Grammar (16) patterns & practices (2) PDC08 (5) Politics (47) PowerPoint (2) PowerShell (34) Presentation (5) Projects (1) HawkWiki (1) Python (4) Quote of the Day (4) Refactoring (1) Research (2) REST (18) Reuse (5) Robotics (2) Rock Band (4) Rome (5) Ruby (23) Ruby on Rails (1) Sci-Fi (2) Scripting (4) Security (3) Service Broker (14) SharePoint (2) Silverlight (18) Social Software (1) Software + Services (2) Software Design (1) Software Factories (11) Software Industry (1) Spark (1) SQL Server (2) Stephen Colbert (1) TechEd (7) TechEd06 (1) TechRec League (1) Television (6) Travel (6) Unified Client (1) Unit Testing (4) USC (1) UX (1) Virtual PC (2) Visual Basic (1) Visual Studio (20) Volta (2) Washington Capitals (34) WCF (31) Web 2.0 (65) Web Services (5) WF (21) Windows Live (23) WPF (7) Xbox (1) Xbox 360 (53) XML (11) XNA (14) Zune (4)
Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.