Rants, rambles, news and notes from another geek

My Powershell Profile Script

The other day a friend asked me for my Powershell profile script. It reminded me of when we used to pass csh/ksh/bash scripts around… but now I’m showing my age.

So, here is mine. Unlike Brad Wilson, I decided to put most of my stuff in this file and not to separate out things into separate .ps1 files. To each his own. There is basically no difference.

(Download link is at the bottom)

########################################################
# Peter's PowerShell Profile (peter@provost.org)
#  v2.0 (2007-01-13)
########################################################
########################################################
# Aliases
set-alias grep select-string;
set-alias wide format-wide;
########################################################
########################################################
# Environment variables
set-content env:\VISUAL '"C:\Program Files\Vim\Vim70\gvim.exe"';
########################################################
########################################################
# Helper Functions
function ff ([string] $glob) { get-childitem -recurse -include $glob }
function osr { shutdown -r -t 5 }
function osh { shutdown -h -t 5 }
function rmd ([string] $glob) { remove-item -recurse -force $glob }
function whoami { (get-content env:\userdomain) + "\" + (get-content env:\username); }
function strip-extension ([string] $filename) { 
[system.io.path]::getfilenamewithoutextension($filename)
} 
########################################################
########################################################
# Prompt
function prompt {
$nextId = (get-history -count 1).Id + 1;
$promptText = "[" + $nextId + "] ?";
$wi = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$wp = new-object 'System.Security.Principal.WindowsPrincipal' $wi
if ( $wp.IsInRole("Administrators") -eq 1 )
{
$color = "Red"
$title = "**ADMIN** - " + (get-location).Path;
}
else
{
$color = "Green"
$title = (get-location).Path;
}
write-host $promptText -NoNewLine -ForegroundColor $color
$host.UI.RawUI.WindowTitle = $title;
return " "
}
########################################################
########################################################
# Custom 'cd' command to maintain directory history
if( test-path alias:\cd ) { remove-item alias:\cd }
$GLOBAL:PWD = get-location;
$GLOBAL:CDHIST = [System.Collections.Arraylist]::Repeat($PWD, 1);
function cd {
$cwd = get-location;
$l = $GLOBAL:CDHIST.count;
if ($args.length -eq 0) { 
set-location $HOME;
$GLOBAL:PWD = get-location;
$GLOBAL:CDHIST.Remove($GLOBAL:PWD);
if ($GLOBAL:CDHIST[0] -ne $GLOBAL:PWD) {
$GLOBAL:CDHIST.Insert(0,$GLOBAL:PWD);
}
$GLOBAL:PWD;
}
elseif ($args[0] -like "-[0-9]*") {
$num = $args[0].Replace("-","");
$GLOBAL:PWD = $GLOBAL:CDHIST[$num];
set-location $GLOBAL:PWD;
$GLOBAL:CDHIST.RemoveAt($num);
$GLOBAL:CDHIST.Insert(0,$GLOBAL:PWD);
$GLOBAL:PWD;
}
elseif ($args[0] -eq "-l") {
for ($i = $l-1; $i -ge 0 ; $i--) { 
"{0,6}  {1}" -f $i, $GLOBAL:CDHIST[$i];
}
}
elseif ($args[0] -eq "-") { 
if ($GLOBAL:CDHIST.count -gt 1) {
$t = $CDHIST[0];
$CDHIST[0] = $CDHIST[1];
$CDHIST[1] = $t;
set-location $GLOBAL:CDHIST[0];
$GLOBAL:PWD = get-location;
}
$GLOBAL:PWD;
}
else { 
set-location "$args";
$GLOBAL:PWD = pwd; 
for ($i = ($l - 1); $i -ge 0; $i--) { 
if ($GLOBAL:PWD -eq $CDHIST[$i]) {
$GLOBAL:CDHIST.RemoveAt($i);
}
}
$GLOBAL:CDHIST.Insert(0,$GLOBAL:PWD);
$GLOBAL:PWD;
}
$GLOBAL:PWD = get-location;
}
########################################################
########################################################
# Custom PS-only path settings
#   use this for directories that contain PS1 
#   files since they generally can't be run 
#   outside of PowerShell
#
function script:append-path {
$oldPath = get-content Env:\Path;
$newPath = $oldPath + ";" + $args;
set-content Env:\Path $newPath;
}
append-path (resolve-path '~/PowerShell Scripts').Path
########################################################
########################################################
# Custom format filters
filter Format-Bytes {
$units = 'B  ', 'KiB', 'MiB', 'GiB', 'TiB';
$ln = [Int64]0 + $_;
$u = 0;
if($ln -eq 0) {
return '0    ';
}
while(($ln -gt 1024) -and ($u -lt $units.Length)) {
$ln /= 1024;
$u++;
}
'{0,7:0.###} {1}' -f $ln, $units[$u];
}
########################################################
########################################################
# 'go' command and targets
if( $GLOBAL:go_locations -eq $null ) {
$GLOBAL:go_locations = @{};
}
function go ([string] $location) {
if( $go_locations.ContainsKey($location) ) {
set-location $go_locations[$location];
} else {
write-output "The following locations are defined:";
write-output $go_locations;
}
}
$go_locations.Add("home", "~")
$go_locations.Add("dl", "~\Desktop\Downloads")
$go_locations.Add("dev", "C:\Development")
$go_locations.Add("scripts", "~\PowerShell Scripts")
$go_locations.Add("addons", "C:\World of Warcraft\Interface\Addons")
########################################################

&nbsp_place_holder;

Download Microsoft.PowerShell_profile.ps1.txt here. You will need to rename the file and put it where $profile is set.

Yet Another PowerShell + WoW Hack

Today I was cleaning out my World of Warcraft SavedVariables folder, which is where WoW addons keep state between sessions. Over time I have installed and uninstalled a number of addons and figured I should clean up a bit.

First I decided to hunt down all files that were for an addon I don’t have any more:

function strip-extension ([string] $filename) 
{
[system.io.path]::getfilenamewithoutextension($filename)
} 
$addonsDir = 'C:\World of Warcraft\Interface\Addons' 
$savedVars = 'C:\World of Warcraft\WTF\Account\<MyAccountName>\
SavedVariables' 
ls $savedVars\*.lua | ? { (test-path (join-path -path $addonsDir 
-childPath (strip-extension($_.Name)))) -eq $false } | rm

Then I realized that I had forgotten to delete the .bak files that may have been created by upgrades, so I created this little command clean those up:

ls *.bak | ? { (test-path (strip-extension $_.Name)) -eq $false } | rm 

Have I mentioned how much I love PowerShell?

(Note: I artificially wrapped some of those lines to make them show up nicely in the blog.)

Interesting Office 2007 Feature

Did you know you could use the mouse wheel to scroll the ribbon bar? I didn’t. Very cool. I’m not sure if I will use it much or not, since I just discovered it, but it could be interesting.

New Tech Term: Buddy Breathing

I don’t know if I invented this application of this scubra phrase, but I seem to have used it a few times lately and people always know what it means.

Buddy Breathing [buhd’-ee breeth’-ing] -noun The process where two people share a single power supply between two laptops, passing the cable back-and-forth between their machines as each battery runs out of power.

Web Service Software Factory December Release

Don, Chris, Dmitri, Larry and the rest of the Web Service Software Factory team have shipped their December release. Congratulations to them for their hard work and for this excellent addition to the space.

This software factory provides support for WCF and ASMX based services. You can get all the details from Don Smith’s post about the release.

A few other intersting tidbits:

Enjoy!

Ummm... What? I'm It? (Aka I've Been Blog-Tagged!)

It is funny. You take a break from blogging (reading and writing) and when you come back, strange things have happened.

Apparently I was tagged by Sam Gentile in the new game of Blog-Tag. And now I am supposed to write down five things about myself that people probably don’t know and then tag five other people.

Ummm… okay…

  1. My first computer was a Ohio Scientific Challenger 4P (circa 1979 - age 10) on which my explorations of computer programming began. From there we moved to an Apple II+&nbsp_place_holder;(1980), then some kind of an Apple II clone that would dual-boot a CP/M z80 machine (~1982). After that came a long line of Intel PC machines that led to my current life as a Microsoft development manager. :)
  2. I played the French Horn as a kid when we lived in Maadi Egypt, a suburb of Cairo (Shout out to my CAC friends!). When we moved from Egypt to Denver, however, there was only a marching band and not a symphonic band, so I quit. Tried piano later, then moved to guitar. Eventually played in a few bands: a “power punk” band called Harrison and a fusion/jazz/groove band called Savvy Backbone.
  3. I never intended to be a professional software developer. In fact, despite having been programming since I was 10, I resisted it at every turn. When I started as a freshman at Colorado State University I majored in Physics, then Political Science, then Sociology, then dropped out and moved to Edmonton to load trucks at my uncle’s company. After a year in the freezing cold of the Canadian north, I decided that perhaps I should go back to college. This time I decided to go ahead and study something easy… like computer programming. But still I resisted. I didn’t want to get a job. I wanted to go to grad school and become a professor. After graduating, however, my good friend Mark Day talked me into submitting my resume to one of his clients. I landed the job and as my dad told me, “Once the money starts coming in, it is impossible to leave it and go back to school.”
  4. Speaking of Egypt, when we lived there I went on a trip to Alexandria with a friend’s famliy. His dad worked for the US Army as a helicopter pilot teaching the Egyptian army about their new Chinook CH-47s. One day Jamie and I went for a walk into the market and found a guy selling really cool WWII howitzer shells that they found in the desert. It didn’t cost much, so we bought it and carried it back to the villa we were staying in. Needless to say, Jamie’s dad freaked out. It was a live shell. And it was 40 years old. Whoops. He kicked everyone out of the house and called the police. We didn’t get our money back. (On a related note, I will never forget when Jamie’s dad took us up for a flight around the desert in one of those birds… amazing!)
  5. At my sister’s wedding, the rehersal dinner was at a nice restaurant here in Seattle (she lived here back then). Since it was Seattle and not landlocked Denver, I decided to have a nice slab of seared Tuna. Mmmmm…. first bite… excellent. Second bite? better. Third bite… ummm… wait… what’s that? There was something small, round, hard, and… metallic in my mouth. I spit it out onto the plate… and it was a bullet. Yes, a&nbsp_place_holder;.38 caliber full-metal jacket slug. Now, I know Tuna are big fish, but I didn’t know that they shot them after hauling them aboard ship. Now I know better. (And yes, I still have the bullet.)

Since I have to tag five more people, here is my list… I wonder how many of them will actually pay attention to this and follow-through?

Can't Connect Live Messenger? Maybe This Will Help

My sister pinged me on IM the other day and told me that she couldn’t connect her new Windows Live Messenger to the main server. “How are you sending me this message?” I asked. “I’m using Windows Messenger–the one build into Windows XP.”

It was interesting, because she could connect the one but not the other. We tried uninstalling and reinstalling. We tried rebooting. We tried lots of different things.

Then I found this thread on the Microsoft Communities site where Jonathan Kay (Messenger MVP) points to a registry hack he wrote that helps most people. In case that file or site goes away some day, I’m duplicating the content of the .reg file here.

To use this, copy the following code into a file called “remove_msgrsettings.reg” and then double click the file to incorporate the change.

**IMPORTANT: **Messing around in your registry can break your computer. If you don’t know what this file does, consider whether you want to use it or whether you might be better off contacting Microsoft Support.

Windows Registry Editor Version 5.00 

[-HKEY_CURRENT_USER\Software\Microsoft\MSNMessenger]

Your mileage may vary… from what I can tell, this registry change doesn’t fix everyone’s issues.

Visited Countries

As long as I’m at it…