Path

There are several locations I add to the path. For these locations, I define short names because they'll be used through the PATH and not directly in the command-line, so I don't need significant names.

Aliases/Functions

I accumulated several useful functions I use frequently (which are sometimes used by other functions in the profile). I use descriptive names for the functions, and if I plan to use them interactively, I also define short aliases. For example, the ElevateProcess() function enables me to run a program with Administrator privileges. I added the alias sudo for quick interactive use:

I also use lots of small functions that really work as aliases with arguments. For example, I often want to kill all the instances of a certain process by name. This can be done pretty easily in PowerShell with: Stop-Process -ProcessName <process name>. You can use the kill alias instead of Stop-Process, but it's still too verbose for me. So I defined a couple of functions to kill Erlang (when working with local RabbitMQ server) and Visual Studio (very useful when installing plugins or when it hangs):

Prompt

The prompt is important in interactive use. You can put pretty much anything you want there. I like to know where I am, so the current directory is a no-brainer. I don't care much about the host because this information can be displayed in the console window title (it doesn't change from one command to the next normally). I also like to know the time a command completed because I often run several lengthy commands in multiple console windows over and over (for example, for deployment after a code change) and I want to be sure of the last time I ran a particular command. There is nothing remarkable about these requirements. The only twist I added to my prompt is that I don't want to display the full path. As a developer, I often work with multiple workspaces with deep directory structures. If I display the full path, it takes a significant part of the line and doesn't leave enough space to type the command itself.

Displaying just the last part of the path doesn't work for me either because I often work on multiple copies of the same project from different branches in parallel. The solution I came up with is to compress most of the long prefix of common trees into a unique mnemonic abbreviation. For example, when I work in the Testing tree, I collapse the path C:\root\ProjectCool\Testing to [X]. A path like C:\root\ProjectCool\Testing\Tools shows up as [X]\Tools and is very easy to distinguish from the same project in the ProjectCool_featureX branch, whose path is C:\root\ProjectCool_FeatureX\Testing\Tools but shows up as [x2]\Testing\Tools\.

The trick is to always have good abbreviations for the trees and branches I currently work on. To set the prompt, you need to have a Prompt() function in your profile that returns a string. In the code below, I keep a hash of path prefixes to abbreviations and I check whether the current working directory matches any of the prefixes  and add the current time, too.

Text Searching (grepping)

PowerShell is all about objects, but it doesn't neglect text processing. The equivalent of the UNIX/Linux workhorse grep is Select-String. For some reason, the PowerShell designers didn't alias it to grep, so it is not as discoverable. I defined my own alias to it: set-alias grep Select-String.

When working with objects, the PowerShell equivalent to Select-String/grep is Select-Object. This is normally more powerful and robust. I defined a couple of functions that help me grep over a bunch of objects by converting each object to its text representation, then using Select-String on the result. I aliased this concoction as sgrep:

Conclusion

PowerShell was designed with the administrator in mind, but it can be amazingly useful in the hands of developers, too. If you find yourself frequently performing tedious and error-prone tasks, consider using PowerShell to make your life easier. PowerShell is powerful, well-designed, and has a good ecosystem and community. It will also be around for a long time given its adoption, ongoing development, and integration with Microsoft products.

Gigi Sayfan specializes in cross-platform object-oriented programming in C/C++/ C#/Python/Java with emphasis on large-scale distributed systems, and is a long-time contributor to Dr. Dobb's.

Update: This article was updated (that is, shortened) on 28 May 2013 due to possible legal action from Roblox for mention of one of their tools in passing.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!