An Absolute Beginner's Babysteps In PowerShell...
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

An Absolute Beginner's Babysteps In PowerShell...

In which i demonstrate that any old DOS hacker can download PowerShell (aka Monad) and teach it to themself, without any prior training or special knowledge, using any old XP machine.

What you need

  1. Windows .Net framework version 2.0
  2. Powershell (2.2 MB download... that's nothing!! Do it! Do it now!)
  3. [Optional: Powershell 'getting started' documentation (i haven't looked at this... that would be cheating)]

Okay -- so kick off the powershell (via the icon in your start menu, or by running 'powershell.exe' [located in C:\Program Files\Windows PowerShell\v1.0 for me.])

You'll be presented with a console, and a prompt.

(continues...)


Here's the prompt...

Windows(R) PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS C:\Program Files\Windows PowerShell\v1.0>

First thing I wanted to try was a few old faithful DOS commands to see what happened:

PS C:\Program Files\Windows PowerShell\v1.0> cd\
'cd\' is not recognized as a cmdlet, function, operable program, or script file
.
At line:1 char:3
+ cd\ <<<<

okay.. cd\ didn't work... not to worry, i soon found that it does work if you include the space between command and parameter, like so:

PS C:\Program Files\Windows PowerShell\v1.0> cd \
PS C:\>     [Success!!]

I'd read enough about powershell to know that there isn't really a 'CD' command. What's happening here is that powershell has a lot of alias's defined, so that one command will be channelled into another.

There's two distinct and powerful uses for these aliases:

  1. They help ease old dos-hacks like me into the powerShell world, by aliasing old command names on top of new cmdLets.
  2. They can provide abbreviations for commands, making you a much quicker scripter.

So, I know that cd works... but I want to find out what the real command is that cd is acting as an alias for...

One way to do this, i figured would be to ask for help with the 'cd' command. Because then of course I'd see the appropriate help for the real command that cd is acting as an alias for.

In dos you can often get help by typping the command followed by " /?".

I tried that first... here was the result:

PS C:\> cd /?
PS C:\d>

Weird result... and one i won't go into here. In any case it wasn't the result I expected. SO i slipped on the old riddling hat, i use for particularly riddlesome situations.

Okay, now every script language other than DOS shuns the use of the slash to indicate parameters. (And if only early DOS had used this same rule, the back slash would never have been needed as a path delimiter...)

SO I tried the same request, this time with a hyphen:

PS C:\d> cd -?

The result was about two screens full of information... telling me how to use the set-location command. Two lessons learnt:

1. Use hyphens to indicate parameters, rather than back slashes!

2. "cd" is an alias for "set-location." Got it.

The help text started like this (i've highlighted one part):

NAME
   Set-Location

SYNOPSIS
   Sets the current working location to a specified location.


DETAILED DESCRIPTION
   The set-location Cmdlet sets the working location to a specified location.
   That location could be a directory, a sub-directory, a registry location, o
   r another location stack.

Groovy! You can use "cd" (i.e "set-location") to navigate around the registry.

Infact here's the first example provided by the help text:

EXAMPLES
   EXAMPLE 1

   C:\PS>set-location HKLM:

Okay -- i recognise those letters, 'HKLM' --> 'HKey_Local_Machine' -- one of the root nodes of the registry! Wicked stuff. I heard about this on a scott hanselman podcast, but wasn't really paying attention at the time.

I guessed that 'set-location' is aliased as 'sl' and hopped into the registry, like so:

PS C:\> sl hklm:
PS HKLM:\>

I was happy to see that the parameter here wasn't case-sensitive. I tried a few variations on sl: SL, sL, Sl and they all worked okay too. Either calling a powerShell cmdLet isn't a case sensitive action, or *all* of those aliases are defined (which seemed unlikely). On a whim, and while think idly of jennifer garner i typed:

PS HKLM:\> alias

And i was happy to see a big long list of all the aliases in the system scroll by...

CommandType     Name                            Definition
-----------     ----                            ----------
Alias           ac                              Add-Content
Alias           asnp                            Add-PSSnapin
Alias           clc                             Clear-Content

Right down to...

Alias           move                            Move-Item
Alias           rd                              Remove-Item
Alias           ren                             Rename-Item
Alias           set                             Set-Variable
Alias           type                            Get-Content

Okay -- i could spend a while talking about those, but instead i'll just bring up a summary of the cmdLets that are aliased by my own 'most-used' DOS commands.

Translating Simple DOS Commands to Powershell cmdLets

DOS CommandPowershell cmdLetA succinct alias:
cdSet-Locationsl
clsClear-Hostclear
copyCopy-Itemcp
delRemove-Itemri
dirGet-ChildItemls
echoWrite-Outputwrite
popdPop-Locationpopd
pushdPush-Locationpushd
rdRemove-Itemri
setSet-Variablesv
typeGet-Contentgc

[sidebar: while i guess i could somehow use 'format-list' to convert the tab-separated list into a html table... i instead used the world's simplest code generator which got the job done quick n easy, like always.

using wscg to
make a html table]

Okay so before I go anyfurther I should pass along this other rule:

They're called 'cmdLets', not commands

You got that?

Getting More Help

Okay i decided i'd like to read some more built in help. I've heard that PowerShell has excellent built in help. But how to get it?

It turns out that 'help' is an alias for 'Get-Help' (the unix-friendly 'man' command also works as an alias for 'Get-Help')

First observation with running these help commands is that the text often scrolls off the screen.

Running help on help (Help -?) tells me this:

"Get-help" and "-?" display help on one page. i.e. they scroll off the screen!

"Help" displays help on multiple pages. i.e. you get this after every screenful:
                                        "<SPACE> next page; <CR> next line; Q quit"

Another great tip in the help about help is to try this:

get-help *

because:

This lists every help file in the system along with what type of
help it is

So, now I've got hundreds of help topics at my fingertips... too many to read and remember in one go. [At this point i went off and wrote 'ShinyPower']

Best of all i know that this 'Get-Help' command is a general way in to the help system.

At this point i've learnt enough simple things that I'm not too afraid of it anymore. I can starting running around in it and really trying things out.

I'll write more about it when i get a chance.





'Farmer Jeb' on Wed, 12 Jul 2006 08:35:18 GMT, sez:

Cool!



'Jonno Downes' on Wed, 12 Jul 2006 09:04:13 GMT, sez:

The world's simplest code generator is pretty nifty, but there's also
the 'ConvertTo-HTML' cmdlet

e.g. to get a html table of aliases, try:

alias | ConvertTo-HTML Definition,Name

And I have a little snippet showing how to actually pop up IE to display the resulting HTML at http://blog.jamtronix.com/2006/07/rendering_html_from_monad.html



'lb' on Thu, 13 Jul 2006 22:01:44 GMT, sez:

brilliant stuff jonno -- i'd read that post of yours but hadn't worked out how to use it very well.

also -- i just noticed the typo in the title of this article:
"An Abolute Beginner's..."

domestic blindness...

lb



'C-J Berg' on Mon, 31 Jul 2006 19:37:16 GMT, sez:

You might want to add these functions and aliases to your profile. It helps old fogies like myself, who are used to CMD.EXE.

# Map "cd.." to "cd .." for convenience
function SetLocationToParent {
Set-Location ..
}
Set-Alias cd.. SetLocationToParent

# Map "cd\" and "cd/" to "cd \" for convenience
function SetLocationToRoot {
Set-Location \
}
Set-Alias cd\ SetLocationToRoot
Set-Alias cd/ SetLocationToRoot



'lb' on Mon, 31 Jul 2006 20:28:12 GMT, sez:

i like your approach here C-J!

i've got a follow up article about profiles... might put this in.

cheers
lb



'Alphonse' on Tue, 19 Sep 2006 23:32:47 GMT, sez:

DOS and hacker are words that do not fit together.



'lb' on Tue, 19 Sep 2006 23:55:22 GMT, sez:

'alphonse' and 'arrogant' seem to go nicely though ;-)



'UntilTheShout' on Sun, 24 Sep 2006 12:08:40 GMT, sez:

Abolute or should it be Absolute?



'blaaaaa' on Fri, 23 Feb 2007 19:39:19 GMT, sez:

No info here.
old old.. and Powershell so much more powerful than some dir-commands ;P



'lb' on Fri, 23 Feb 2007 19:46:00 GMT, sez:

hey 'blaaaaa' -- yes powershell is much more powerful than what we've done in this article... but this is called 'an absolute beginner's baby steps in powershell' ... so you shouldn't expect it to do anything other than take very very small and awkward steps...



'Nabizzle' on Wed, 11 Apr 2007 17:26:35 GMT, sez:

Agreed. I just installed Powershell, and was looking for something interesting to read that had some info in it. Besides, it is always good to look at how someone else learns something to improve your techniques.



'Jay' on Wed, 19 Sep 2007 14:46:46 GMT, sez:

nice job!
could you pls refer some pages where i can get the basic steps fro scripting since i'm very new on scripting



'MDH' on Thu, 18 Oct 2007 19:30:10 GMT, sez:

Nice! Thanks for intro info. Make more, please.



'Buddaman' on Thu, 24 Apr 2008 12:23:30 GMT, sez:

Great Info, Can you post more on basic scripting with powershell, i am a newbee and don't know much, but i know i luv it ;-) - how 2's etc pls..




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

Coding Koan: the power of one Coding Koan: the power of one
Behavior Driven Development: As Human As Possible Behavior Driven Development: As Human As Possible
What To (Really) Do If You Find Out Your Parents Are Using Vista (redux) What To (Really) Do If You Find Out Your Parents Are Using Vista (redux)
What To Do If You Find Out Your Parents Are Using Vista What To Do If You Find Out Your Parents Are Using Vista
Sample Code From Text-Adventure Game Platforms Sample Code From Text-Adventure Game Platforms
TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities
The laptop compubody sock The laptop compubody sock
Everything that's bad for you is suddenly good for you! Everything that's bad for you is suddenly good for you!
Everything I know about Code Reviews I learnt from Star Wars (and JCooney) Everything I know about Code Reviews I learnt from Star Wars (and JCooney)
Syntax highlighting of strings Syntax highlighting of strings
Google AppEngine: evil virus or viral evil? Google AppEngine: evil virus or viral evil?
Workflow software: I'm calling the bluff. Workflow software: I'm calling the bluff.

Archives .: secretGeek :: Complete Archives :.
25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Top 10 SecretGeek articles Top 10 SecretGeek articles

Downloads

TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 2.5: with password protection

ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Next Action NextAction
Managing the top of your mind



[powered by Google] 

Thai Erawan, Brisbane Restaurant, delicious thai food in paddington Thai Erawan, Brisbane Restaurant
World's Simplest Code Generator (html edition) World's Simplest Code Generator
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker
How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)


Recommended blogs

Jeff Atwood
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Joel Pobar
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

LinkedIn profile
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy