Lately I’ve been getting acquainted with Windows PowerShell, which is something Windows has needed for a long time. Finally, there’s a real shell for Windows that supports scripting in a format other than horrific batch files.
At work, for whatever reason the powers that be have set our %HOMEDRIVE% and %HOMEPATH% environment variables to a networked drive. This would be a good idea, but the problem is this drive has a pretty low quota applied and I managed to fill it up within a week or so. Basically, people use it as a convenient public share directory. I mention this because changing the environment variables in this way has caused me much grief. cmd.exe defaults to %HOMEDRIVE%\%HOMEPATH% when it starts, so every time I open a command prompt I end up in this stupid useless directory. PowerShell is not immune to this either.
I noticed that my PowerShell always started here and that the $home variable seemed set to it as well. I set up a profile to change my location to the correct place, but had trouble setting the value of the $home variable since it is read-only (or constant, according to the error message). Luckily, there’s a way to tell PowerShell, "I know what I’m doing." and the Set-Variable cmdlet takes a -force parameter. So in the end, my script ended up looking something like this:
Set-Location "C:\Documents and Settings\username\My Documents" Set-Variable -name home -value "C:\Documents and Settings\username\My Documents" -force
I could probably make this more OS-agnostic by using the .NET Environment class, but that’s a project for another day.