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 "$HomeScripts_profile.ps1"

# Run the $PROFILE script
. $profile

By default, PS uses the user’s personal directory (c:usershpierson 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 $homeScripts folder and runs it.

My $PROFILE script does several things of note:

  • It adds the aforementioned $homeScripts 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:winsys32.
  • 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.

Morning Coffee 167

  • If you’re a gamer, you’re probably already well aware that E3 is this week. The Too Human demo has already been released. I have a friend who’s been working on “something” that will be announced today (I think).
  • Live Mesh folks pushed out an update Friday. Among the new features is the ability to sync folders among peers but NOT up to the cloud. This is cool because it means I can sync my many many GB of pictures and music on my home machine backed up with Carbonite. This means I can sync them without blowing thru my 5GB Mesh storage limit.
  • It looks like there’s a new F# drop – 1.9.4.19but as usual there is no announcement or details as to what’s new. Release notes guys, look into it.  Update: Don Syme blogged the release, and it’s pretty minor. a .NET FX 3.5 SP1 bug fix, a fix for Mono, and they removed WebRequest.GetResponseAsync to make F# work on Silverlight. And the release notes are in the readme. My bad.
  • Speaking of F#, it was “partially inspired” by OCaml, so when I see papers related to OCaml, I immediately wonder if I an apply the described techniques to F#. “Catch me if you can, Towards type-safe, hierarchical, lightweight, polymorphic and efficient error management in OCaml” is one such paper. (via LtU)
  • Speaking of functional programming, Matthew Podwysocki posted a bunch of FP links as well as a Code Gallery Sample on FP in C#. Good stuff.
  • As per Scott Guthrie, it looks like there’s a new ASP.NET MVC drop coming this week.
  • Based on posts by Ted Neward, Dare Obasanjo and Steve Vinoski, Google Protocol Buffers sounds like it’s going to be a dud. Note, I haven’t looked at it depth personally, I’m just passing on opinions of some folks I read and trust.
  • Speaking of Dare, both he and James Hamilton take a look at Cassandra and come away impressed. I wonder how easy it is to code against from Python and/or .NET?
  • Bart de Smet has a cool sample of calling out to PowerShell from IronRuby via the backtick command. Pretty cool, but it would even cooler to show how to call out to PS and return .NET objects to Ruby (though that would probably not be spec compliant for the backtick command).
  • Here’s a MS code name I had never heard before – Zermatt. It’s “a framework for implementing claims-based identity in your applications.” (via Steve Gilham)

Morning Coffee 166

Yes, I realize it’s been a while. I tried in vain to catch up with my blog reading after my Hawaii vacation and finally just gave up and hit “mark all as read”.

Dynamic Languages

  • There’s a new version of the DLR hosting spec available (doc, pdf). The DLR implementation is still in motion, so there are some inconsistencies between the spec and the code, but the spec should give you the high level overview you need if you want to host DLR languages inside your app.
  • Oleg Tkachenko recently joined the dynamic languages team. He’s the creator of the Interactive IronRuby Web Shell, an IronRuby version of Try Ruby. Of course, it’s not as cool as using SL2to execute the code directly in the browser. Michael Foord has his Python in the Browser and my teammates John and Jimmy demoed a Silverlight version of Try Ruby @ TechEd.
  • Jim Deville, also of the dynamic languages team, recently started blogging.
  • I have a new boss, Dave Remy. He doesn’t have a blog – yet – but you can follow him on Twitter as daveremy. When Twitter is actually working that is.
  • There’s a new homepage/wiki for IronRuby though I’m not sure why there’s a picture of Matz wearing a Python shirt on the home page.
  • My teammate Jimmy Schementi provides some “continued hope” for a better (heck, I’ll take current) ASP.NET and ASP.NET MVC story for DLR languages.
  • Via Michael Foord, sounds like IronClad is making good progress. V0.4 can run the bz2 module “in its entirity” (maybe run a spellcheck on your site, guys?) and now apparently, it’s now able to load numpy.core. Very exciting!

Other Stuff

  • Pat Helland, who has blogged even less than me for the past few months, has a post up about controller and doers in the IT department. After 18 months in MSIT, put me in the doer camp, please.
  • The F# team has pushed out a spec for v1.9.4 of the language. Don Syme says it’s not official, but it’s a huge improvement over the old informal spec
  • Speaking of F#, my friend Matthew Podwysocki recently published FsTest, a testing DSL for F#. I wrote about F# unit testing as part of my PEG parsing series, and I really like the direction Matthew has taken this project. You can pull it down from CodePlex.
  • When I did my PEG talk @ Lang.NET, Gilad Bracha mentioned I should check out oMeta. It looks really cool, though with the job change I haven’t had the time to play with it. Now I discover that Jeff Moser is working on a version for CLR called oMeta# that I’ve got to spend some time with. And in the comments to that post, I discovered pyMeta from Allen Short, though it apparently doesn’t work on IronPython (must investigate why).
  • James Kovacs introduces psake, a PowerShell based build automation tool which uses a rake-inspired internal DSL syntax similar to one I blogged last year. I’d love to see this take off, but given MSBuild’s tool integration, I wonder if that’s feasible.
  • I upgraded my home wireless network almost exactly a year ago. I’ve been happy with the range and coverage, but not so happy with the Buffalo Tech firmware. The built-in DHCP server is pretty flaky. So I upgraded to the open-source Tomato firmware. Upgrade was smooth, though I did need to reset my cable modem. But even that was smooth – Comcast has an automated service for that now,

Morning Coffee 162

  • Another nice thing about the new job: I’m working in the vicinity of some good friends. I was over in building 42 yesterday and made it a point to stop by Pat Helland's office yesterday and spend an hour or so chatting about the new gig. Pat is down the hall from David Hill, whom I worked with on Architecture Strategy. Back in my building, we’re down the hall from the VSX folks including my friends Ken Levy and Gareth Jones. I’m sure there are more folks I know around, but hey it’s only my second week!
  • I’m a big fan of Carbonite, which I use to back up all the digital media on my home computer. With two little kids, we have lots of digital photos as you might imagine . However, one thing that bugs me about Carbonite is that it doesn’t back up video files by default, you have to go in on a folder by folder basis and select “‘Back up Video files in this folder” from the context menu. Given how much trouble this “feature” has given me, I imagine less techie folks don’t even realize their video files aren’t getting backed up. However, I will say the latest version of the Carbonite Software at least makes it easy to find files that aren’t backed up. A quick sweep revealed around a dozen folders that had un-backed-up video files in them, which I promptly fixed.
  • The big news yesterday was the new Google App Engine, which looks to give you access to virtualized infrastructure that sounds similar to what GOOG is rumored to use internally. I like Dave Winer’s comment that this enables “shrinkwrap net apps that scale that can be deployed by civillians.” Given Google’s history w/ Python – Python’s BDFL Guido van Rossum works there – it’s no surprise that Google App Engine (GAE?) runs on Python, though apparently they “look forward to supporting more languages in the future”. I’m guessing “more languages” == Ruby, maybe Erlang too.
  • I wonder if/how Google App Engine will affect Ruby on Rails momentum? If there’s a significant lag before App Engine supports Ruby, will that drive developers to Python web stacks like Django? (Django is included in “the box” with App Engine)?@ PyCon, I was surprised at the intra-language animosity I observed. I wonder how many Python developers are secretly hoping Google never ships Ruby support. I highly doubt Google would do that – they want to tap the exploding RoR market like everyone else – but I’d bet it would really take the wind out of Rails’ sails if they did.
  • Today’s Michael Foord Link: Embedding IronPython 2, Examples of the DLR Hosting API. You can read the DLR Hosting spec, but it’s pretty out of date so Michael’s article helps fill in some of the gaps.
  • Looks like PowerShell has gotten the open source community treatment in a project called Pash. While I’m sure others are excited about PS on Linux or Mac, I’m excited to see PS running on Compact Framework. I wonder if it would work with XNA?
  • Speaking of XNA, XNA Console is a new CodePlex project that provides an IPy console to manipulate your XNA based game on the fly. Python is no stranger to game development – Civ IV for example provided mod capabilities via python. Alas, the compact framework can’t run IPy today, so neither can XNA on Xbox. But wouldn’t it be cool to hack your game in IPy running on a 360 using the messenger kit? (via IPy URLs)
  • Bart De Smet gets functional, writing type switch and pattern matching in C# 3.0. I guess it works, but it sure is ugly. Why not just use F# and be done with it?
  • Soma announces that the VC++ Feature Pack has shipped. Somewhere, I assume, there is much (some?) rejoicing.

WebDev.WebServer PowerShell Function

In experimenting with NWSGI yesterday, I found I wanted the ability to launch the development web server that ships with Visual Studio (WebDev.WebServer.exe) from the command line. I hacked up the following PowerShell function and dropped it into my $profile so I can easily launch the web server in any directory any time I need. Thought I’d share:

function webdev($path,$port=8080,$vpath='/')
{
    $spath = 'C:\Program Files\Common Files\microsoft shared\DevServer9.0\WebDev.WebServer.EXE'
    $rpath = resolve-path $path  
    $params = "/path:`"$rpath`" /port:$port /vpath:$vpath"  

    $ignore = [System.Diagnostics.Process]::Start($spath, $params)  
    "Started WebDev Server for '$path' directory on port $port"  
}

There’s probably an easier way to launch an exe with parameters than Sys.Diags.Process.Start, but it works. Using resolve-path is the key, that lets me pass in a relative path on the command line, but the script converts it to an absolute path in order to pass it to the webdev server. Also, I’m not sure I should have hard coded the path to the exe, but again it works and it’s not like it’s tough to change.

Enjoy.

Update: Tomas Restrepo pointed out an easier way to start the process:

&'C:\Program Files\Common Files\microsoft shared\DevServer9.0\WebDev.WebServer.EXE' "/path:$rpath" "/port:$port" "/vpath:$vpath"

I couldn’t figure out how to correctly launch the exe when the physical path to serve has a space in it. Thanks Tomas.