Yesterday I recevied an email about some code snippets in my PowerShell profile that I’d mentioned on StackOverflow. While trying to retrieve them; I noticed that I hadn’t put them on to my newly-installed Windows 8.1 machine; so I thought it was worth sharing them here while I was copying them from my work machine anyway.
I don’t use PowerShell for scripting much now; I’ve been using FSI (type safety FTW). However, I do still think PowerShell is an excellent shell, and the ability to connect remotely to servers is incredibly useful for executing our deployment/IIS setup scripts/etc.
Adding Colour to Hostnames in PowerShell Remoting Windows
With some help from StackOverflow, I have some functions to help me connect to frequently-used servers, highlighting the hostname depending on whether the server is local/staging/live (green/yellow/red). They’re all prefixed with R- so I can type R- and then tab through the different servers.
Launch Kiln/BitBucket/Whatever from Shell
The first thing Iusually do after pushing changes to Kiln, is open up the web app and raise code reviews. So; I added a command “Kiln” which just launches Kiln at the correct repo page for where I am. The Kiln extension can do this (“hg kiln”), but we’ve had bad experiences with the Kiln extensions not working on recent versions of Mercurial (due to PyWin32 dependencies) so most of us have them all disabled (except KilnAuth).
Launch Visual Studio for Current Project
Because I switch between repos a lot (different versions of our product), it’s a pain to change directory in PoSh and also navigate to the new solution in Visual Studio. So I simply close Visual Studio and type “VS” when in the correct directory for the new branch to launch Visual Studio for any sln file in the current folder. This could be extended to recurse down (or up), but I found it most convenient just working in the immediate folder.
Launch Windows Explorer for current folder
Why type “start .” when you can just type “e” to start explorer in the current folder?
Launch default browser for current folder via web server
Sometimes you have a static html file you want to load up in a browser to hack on and test, but some browsers (like Chrome) won’t let you pull in scripts for file:/// paths. This function (which requires Python in your PATH) launches the builtin Python web server for the current folder and fires up your browser. If you pass an optional filename
then it’ll load that fie in the browser.
Run Mercurial Commands with Less Typing!
And while we’re at it; why have to type “hg” all the time?
Add Visual Studio / .NET Tools to PATH
I like the Visual Studio Developer Command Prompt because it has lots of useful stuff in PATH; but trying to get this into PoSh sucks. You can’t call the old .bat files because they won’t update PowerShells PATH. Re-implementing them is insane, so it’s easier to just hard-code the important few, and add/update as required!
Clean all folders with MSBuild
Create a PSDrive for Code Folder
To make it easy to always get back to my root code folder, I’ve created a PDrive, and change into it at launch.
I think that’s all the useful stuff incurrently in my profile that might be handy to others. Do post your own useful snippets in the comments!