There's this little "CD" utility that I iterate on every once in while but has become one of my favorite PowerShell tools on Windows.

Not because it's really that great, but more because there are some navigation habits I acquire over on a Mac in a ZSH terminal that become challenging to not have on a Windows PowerShell terminal and each time I iterate on it, it synchronizes my workflows on both Mac and Windows environments.

In my previous update I added support for typing cd ...N where cd .. will go up 1 directory, so cd .... will go up 3 directories.

Well today I found out that I can declare a function in powershell with a name .. - WHO KNEW?

For example if you pasted the following into your PowerShell terminal: function ..() { echo "HELLO"; }; ..

This would define the function .. as well as run it and print out HELLO.

This was a fantastic stumbling on my part because on my Mac I often go up 1-n directories by typing .. at the terminal or ..... <-- however many I want to go up.

A while back I wrote about a replacement for the cd command on powershell that I wrote which provides some fun features such as history tracking, support cd'ing to a folder when a file path is given, etc... It's been a while since I've touched this helpful little tool which sometimes I even forget I wrote it because it's something that's used practically every day and "it just works".

It now supports cd ...

Well, today I threw a quick feature into this utility that I've become accustomed to using in zsh on my Mac.

On many *nix command prompts you can type something like cd ..... This command translates indo cd ..; cd ..; cd .. (but executed as one command). The first .. counts as one directory and then each ane every . after that counts as another directory up the tree.

So now within PowerShell when I cd down into a deep folder structure, I can now use cd ....... to go back up N folders.

Some background

I leverage these tools in many projects, both at work and on my own projects. I recently noticed that the muscle memory I've developed was slowing me down as I switch between projects and inadvertently use the wrong tool in the wrong project.

When working on one project all day and typing rake test throughout the day, then switching to another project not using rake I found myself still typing rake test even though I needed to type gulp test, or grunt build. This really messes with my flow. All I wanted to do was test or build the current project.

What is it?

In short, it's a very simple abstraction on top of your usual task runners like GulpJS, GruntJS, rake, etc that allows you to accidentally type the wrong tool and still accomplish the desired task with the right tool.

By using aliases in either PowerShell or Bash we can route, rake, gulp, grunt, etc to CommandAllThings which will inspect the current directory, determine the correct tool and execute your task(s) with the correct tool.

This is great. Now when I type in the wrong command rake test in a gulp project, I don't get slapped in the face with an error saying "cannot find a rakefile." Instead I continue on my way like I knew what I was doing.

Does it only work on Windows or on the Mac?

At the moment it works on both.

For Windows I have an implementation in Powershell.

For the Mac you can alias to a Bash script.

Download at GitHub!

What's next?

I'd like to look at adding other tools to the list. You can check out the project for other task runners I know about. And if you have any thoughts on how to detect the other types, drop me a note in the issues (or consider sending over a pull request).

I'd also love to get some feedback on how to best or more easily get this into peoples's development environment. For now it's pretty manual and since it's all about your profile, a bit personal, so probably a good thing that it's not automatically installed.

After a fresh install of Visual Studio, I often re-configure some options and setup keyboard bindings. This tends to be a bit of a pain as each and every time I have to remember what setting is in what U.I. configuration pane and how do I find/configure that again? I also will often forget about certain settings until the point I need them and it really breaks my coding rhythm down to go hunt for and reconfigure these options.

I recently found a simple solution to get my VS just the way I want it.

P.S. Some of you may want to tell me all about the VS import/export settings, and you may be right, but I just haven’t spent the time to use/understand/easily find a way to integrate it into my ‘flow’.

So I present to you “MY happy path” to Visual Studio Environment configuration.

1. Install the NuGet Package Manager Extension

You’re going to probably install this extension anyway, so go-ahead and install it now if you don’t already have it… We need this so you can get access to the NuGet Package Manager Console

After your search, most examples you find will be VB macros and as it turns out VB (in this case) translates nicely to PowerShell (EX: VB Macro: DTE.Commands.Item("File.Close").Bindings = "Global::Ctrl+W"

A while back I wrote about a Slightly modified “CD” Command for PowerShell. Since that point, I’ve made a number of updates and would like to share them.The first change I made was to move the “CD” script in that post to a new location. I’ve greatly extended how I setup my development environment and how my PowerShell environment is initialized.

If you’re interested in how I setup my PS profile, take a look at the readme. Combine the setup with some Chocolatey and some BoxStarter and you’re on your way to an amazingly automated development environment setup.

Now on to the features I’ve added to the CD command.

I often would do something like “CD $Profile” (which is a no-go because that is a file not a directory, but my intent was to get into the directory where the $Profile file lived).

So I updated it so if you try to CD to a file, it will just take you to the directory where the file resides.

If you try to CD into a folder that doesn’t exist, it now prompts you to create it. You could bypass the prompt with a -force|-f flag.

Lastly I fixed an issue reported by a commenter in the previous post about trying to CD into a folder with the same name as a history index. If you had a folder named “4” and you typed “CD 4” it previously wouldn’t take you the 4 directory, but instead lookup item 4 in your CD history and take you there. Now if you specify a number and that number lives in the current directory it will take precedence over your history value. (To be fair, I only added this feature, and even since I originally wrote about the CD command I have yet to run into a need for this case. Probably because I don’t name my directories with numbers).

Something I had only recently noticed is this script appears to work with other PowerShell drive providers. I only did a cursory test but can CD into the HKLM:\Software\MyTestFolderThatShouldntExist and it will prompt to create the ‘folder’. If I say yes, I end up with a new registry folder. Not sure how useful that is, but with the abstraction layer PowerShell drives give us it’s interesting.

If you haven’t heard of it, you’re about to be delighted. Every developer (at least those on Windows) should know about this project, if for nothing more than to make life setting up your dev machine a piece of cake.

What is Chocolatey?

Chocolatey NuGet is a Machine Package Manager, somewhat like apt-get, but built with windows in mind.

If you’ve not ever used a linux machine or understand what the power of an “apt-get” like tool is, well It’s basically the simplest possible way to install an application to your machine.

What could be simpler than finding the app’s website, download the app, and next, next, next through the the installer? How about just typing “chocolaty install notepadplusplus” at a powershell command prompt? That simple little command will download and install Notepad++ right on your machine with virtually no need to interact with the installer. AWESOME!!!

Disclaimer!

I know there are other installer applications out there that aggregate and install different programs; however, to be honest, I don’t use any of them. I am also going to assume that most of them aren’t catered to the windows developer (maybe I’m wrong). Either way I like this project and I’m just trying to share it with the community. So There…

Ok, my above salesmanship is a little loud mouthy, but maybe your interest is peaked enough to give it a try.

How do I install Chocolatey?

It’s about as simple to install Chocolatey as it is to use Chocolatey to install other applications. One single powershell command. Just paste the below command in your powershell prompt and let er rip.

First, make sure you have your powershell environment set to “Unrestricted”.

I’m not so sure this fits in the “elegant code” theme, but it’s a “fun with code” topic that someone might enjoy. Especially if you have a little one.

My 4yr old is learning how to spell small and simple words like her name, “Mom”, “Dad”, etc, and continuing her exploration with letters on the keyboard. She’s been banging on a keyboard since her early years on babysmash. In fact I came home one day to find my monitor turned 90 degrees and about every possible admin window open in the background because of certain key combinations were not trapped by babysmash. But I digress…

For a while she was typing some text into notepad and asking me what it spelled.

“ajlkjwelsl” –> What’s that spell daddy?

I then thought it would be fun if the computer could give instant feedback about what she typed and in a matter of a minute or so I whipped up this little “game” which we had fun playing for a bit.

Background

In my previous job, I spent all my development time in a Linux environment. Was rather impressed at how much could get done at the command line, and how efficient some of those tasks became. My next job was based on Windows and mostly the Microsoft stack of development tools. This meant I pretty much left the command line behind. That was, until, I started using git. And since I wanted to learn PowerShell, I used PowerShell to execute my git commands.One thing that has bugged me for a while is simply moving between directories. Even with tab completion, all that typing is a still quite annoying. Especially if you jump between a set of similar directories. One feature from the Linux CD command that I missed was “CD -". This command in Linux can be used to jump to the previous directory (and then back again). One limitation of this command is it only could jump back to the previous directory, and it did not retain a memory of recent directories. There may be something better in Linux that I don’t know of, but I’m basing this on a limited experience a number of years ago.So I threw a question out on twitter. After several tweets back and forth, @cwprogram threw an interesting spike at me.http://pastebin.com/xwtkn0amAlthough this wasn’t exactly what I was looking for, it contained enough of what I needed to spark my curiosity to write a version of my own.And so a little script was born that I’m now using to replace the “CD” command in my PowerShell runtime.

What does this do?

After you get it installed (see install steps below), when you type “CD” with no parameters at the command prompt. It will list up to 10 of the most recent distinct paths you’ve been to recently. This list also gives an index lookup number that you can use as a shortcut to jump to that path.Example:

C:\code> cd 1) C:\Users\jasonj 2) D:\temp C:\code> cd 2 D:\temp>

You can continue to use the “CD” command to do your usual changing directories. Now you can quickly get a history of where you’ve been, and quickly jump to any of those previous histories without typing the entire paths again.It defaults to only showing you the last 10 distinct items, but if you find yourself needing to go back farther than that, you can use the following command to list more than 10 items.

Comments

Jason.Jarrett

Thanks for the tip.

Bartek Bielawski

If you want some more *nix features, including cd - you may want to look at pscx.codeplex.com - I made mistake of ignoring what it has to offer and also re-invented cd- in my module. ;) IMO pscx should be added to win build by default. ;)

Every once in a while I run across a list of url’s that for whatever reason I want to make sure are valid. I have not yet found a good way to do this in PowerShell and finally hit the case where I decided to just write what I needed.

I was playing around with some Silverlight profiling the other night to see if I could find any obvious issues with my open source project StatLight and wound up writing a little script in PowerShell I hoped someone might find helpful.

But I didn’t know you could profile a Silverlight app.

I didn’t until I started doing some digging online. Now, the Silverlight profiling story (at least from what I’ve seen/tried) is not near the easy of say Redgate ANTS Performance Profiler. However, it’s do-able. (And with this helper script – hopefully a little simpler?)

How can I profile a Silverlight Application?

I would recommend you read the following blog, which explains one way of doing it very well.

Today I was testing a long running task where my basic scenario was to execute at the PowerShell prompt.

C:\dev> LongRunningTask.exe > Test1.txt

C:\dev> LongRunningTask.exe > Test2.txt

C:\dev> LongRunningTask.exe > Test3.txt

Change some code and do it all over again.

C:\dev> LongRunningTask.exe > Test4.txt

C:\dev> LongRunningTask.exe > Test5.txt

C:\dev> LongRunningTask.exe > Test6.txt

Although seemingly simple and really not that tough. It became really inefficient because I didn’t like sitting there waiting for it to finish. I was off reading blogs, responding to email, investigating other coding issues and generally forgetting about the long running tasks in the background. On top of that, when I came back to it I couldn’t remember what the last run was (was that test 2 or 3 or 5?) without querying disk to see what it was.

Sine I had all this time to kill in between test runs, I wrote a quick PowerShell script that would fully automate test 1,2,3 and stop. This was great and the first time I ran it was much more efficient as I didn’t have to keep checking back to see when to start it up again.

However, I not only had time to kill, but now I had 3 times that time to kill. I decided to elaborate on the script. I searched the tubes for a way to play a sound when a task was done, so I could be notified as to it’s progress, and completion.

I stumbled upon this blog, which was great for showing me how to play sounds within PowerShell.

Basically using the built in Microsoft speech api’s you can get PowerShell to talk to you. This was just too much fun for the day.

At the end of the day the script below is basically what I was using. Don’t have too much fun, as you might not get any real work done…

The content of this blog isn’t necessarily top notch, but I just had to blog about it, as I was very impressed as to how easy it was to work with the speech api’s. Never knew it could be so easy to make a script verbally interactive.

#*************************************************************** #***************** How many times to run task? ***************** #*************************************************************** $iterationCount = 3

One of the tools I’ve used the last half of a year and really enjoyed is the C# specification extension methods when writing unit test assertions. If you’re looking for a little more background on the topic, I wrote about Fluent Specification Extensions in a past blog.

Recently I wanted to execute a PowerShell script to do some automated functional testing. I wanted to execute an application and apply some assertions on the output of the software(basically running a console app, parse the xml output and assert on values in the output).

FYI: I’m very new to PowerShell, so any suggestions on how I implemented the below are welcome…

I’ve seen a couple examples of writing test assertions in PowerShell out there. One example is PSUnit; however, this seemed a little heavy for my needs and not quite the syntactic sugar I was looking for.

Besides the syntax flavor I was desiring, another thing I wanted to do was leverage the power of NUnit.Framework’s assertion capabilities. I like the error messages generated when strings and other objects fail the assertion.

Now that the extension methods have been defined and loaded into the runtime, NUnit.Framework is loaded, we can now use the methods on any object that inherits from System.Object (which, as far as I know, is everything in PowerShell).

And now, everything you need in one script (if you have the xml extended type file saved somewhere…)

I was attempting to use PowerShell to compile a group of *.cs source files – needing the flexibility of programmatically swapping out dependent assembly references at compile time depending on certain build conditions… Don’t want to get too much in to why I needed it, just that it is doable – (more painful than initially expected), but still possible.

First let’s get a csc command we want to compile.

Second let me state that this was more of an exercise in wanting to learn PowerShell and there probably other ways of accomplishing what I needed, just seemed like a good time to start down the painful learning curve. Also note, I’m not a CSC compiler pro – I haven’t analyzed each of the “options” and weather it’s right/wrong/best practice – it just works… (thanks to Visual Studio & MSBuild for hiding how we actually should use the compiler)

Ok take a simple csc compile command – (In Visual Studio – File –> New Project -> ClassLibrary1 as a good starting point). Compile the project & check the build output window. You’ll get an output similar to the below.

# this drove me crazy... all I wanted to do was execute # something like this (excluding the []) # # [& $csc $opts] OR [& $cmd] # # however couldn't figure out the correct powershell syntax... # But I was able to execute it if I wrote the string out to a # file and executed it from there... would be nice to not # have to do that.