Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Windows powershell. aliases.

Posted by peeterjoot on April 29, 2010

 

We’ve been instructed here at work that we must now install Windows XP service pack III, since service pack II is going into a non-update mode.  Four reboots later and much time elapsed, I’ve got the service pack and various other recent windows updates that I’d neglected installed.  One of these is the Windows Powershell.  Well, as a command line kind of guy, I couldn’t turn that down.  Has windows gotten its act together and finally produced a usable shell that one wouldn’t have to run something like the slow cygnus bash to avoid cmd.exe?

Here I’ll make some notes about first impressions.  One of these is that we have aliases, some of which appear to be to help a Unix person feel at home:

PS C:\Documents and Settings\Administrator> alias ls

CommandType     Name                                                Definition
———–     —-                                                ———-
Alias           ls                                                  Get-ChildItem

Interestingly, the old style DOS dir command is also an alias:

 

PS C:\Documents and Settings\Administrator> alias dir

CommandType     Name                                                Definition
———–     —-                                                ———-
Alias           dir                                                 Get-ChildItem

What is this Get-ChildItem.  It appears to be what the powershell help calls a command-let, and it’s behaviour can be viewed with:

 

get-help Get-ChildItem

 

or

 

get-help Get-ChildItem –detailed

 

or

 

get-help Get-ChildItem –full

 

This help text shows that this command-let does a lot more than dir or ls.  For example, it has the capability to query the registry, and do recursion, as well as having exclusion and other flags.  For example, if I don’t want to see the *.txt files in my current dir, I can run:

PS C:\Documents and Settings\Administrator> Get-ChildItem -exclude *.txt

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and Settings\Administrator

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-        11/24/2008  10:34 AM            (null)
d—-        11/24/2008  10:34 AM            .jnlp-applet
d—-        11/24/2008  10:34 AM            Bluetooth Software
d—-         4/29/2010  11:16 AM            Desktop
d-r–         4/29/2010   1:00 PM            Favorites
d—-        11/24/2008  10:35 AM            IBM
d—-        11/24/2008  10:33 AM            My Documents
d—-         2/10/2010  11:11 AM            SametimeMeetings
d—-        11/24/2008  10:33 AM            SametimeTranscripts
d-r–         9/17/2009  12:39 AM            Start Menu
d—-          2/9/2010  12:18 PM            Tracing
-a—         2/28/2010   9:50 AM      11332 gsview32.ini
-a—         7/30/2009   2:59 PM      46520 install.xml
-a—         3/22/2010   6:15 PM      17437 _viminfo

This little beastie of a command appears to be something of a hybrid of ls, and find.

 

How do we create an alias?

get-help alias | more

This help shows that we can do some interesting queries, like seeing what aliases all map to the Get-ChildItem above.  Example:

 

PS C:\Documents and Settings\Administrator> get-item -path alias:* | where-object {$_.Definition -eq "Get-Childitem"}

CommandType     Name                                                Definition
———–     —-                                                ———-
Alias           gci                                                 Get-ChildItem
Alias           ls                                                  Get-ChildItem
Alias           dir                                                 Get-ChildItem

and also provides an example of creating one:

 

PS C:\Documents and Settings\Administrator> new-item -path alias:np -value c:\windows\notepad.exe

CommandType     Name                                                Definition
———–     —-                                                ———-
Alias           np                                                  c:\windows\notepad.exe

The syntax isn’t exactly obvious seeming like the Unix alias builtin, but it appears usable.  Now that we have an alias, putting it somewhere for use in our shell start up is the next logical step.  This appears to do the job

 

PS C:\Documents and Settings\Administrator> export-alias -name np,note blah
PS C:\Documents and Settings\Administrator> cat blah
# Alias File
# Exported by : Peeter
# Date/Time : Thursday, April 29, 2010 2:36:40 PM
# Machine : JOOT
"np","c:\windows\notepad.exe","","None"
"note","c:\windows\notepad.exe","","None"

 

with import-alias blah, to get them back later when we start a new shell again. 

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

 
%d bloggers like this: