PowerShell and cd..

When I just started to learn PowerShell, my first book was Learn Windows PowerShell in a Month of Lunches

I remember one very interesting quote from it

Technically, Cd.. is incorrect because it doesn’t include a space, and Cd .. is correct. In reality, PowerShell v2 catches the Cd.. error and will do the right thing (move
up one level in the directory hierarchy) because that’s such a commonly typed command, but that’s the only exception that PowerShell will catch that way for you. It
won’t catch something like Dir.. so it pays to be careful with those spaces.

According to this quote I thought that PowerShell interpreter has hard-coded special cases to interpret cd.. command. And I was always wondering how exactly that’s happening.
Correct me if I’m wrong, but there are no articles in Internet except this one that explains how it works…

Recently I suddenly discovered how this works… Actually my understanding (and probably author’s) is not right. There is no catch for the error. It is just simple

Let’s execute the following command

(Get-Command cd..).Definition

And the result is

Set-Location ..

You see? That’s simple. PowerShell just has a separate function to handle this case

If we type

Get-Command -CommandType Function

we will find many other helper functions, e.g. command C: just equivalent for Set-Location C:

So there are no magic there.

If we want to have dir.. command working as well, we can just write

function dir.. { dir .. }

That’s it. Now dir.. works as well.


About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s