Tommy’s Blog

Photography, technology, and a little bit more from Tommy Williams

Archive for July, 2006

Translating from cmd.exe to PowerShell: dir

30th July 2006

After a long hiatus from Monad, err, PowerShell, I have started working with it again now that Release Candidate 1 is available.

One thing that frustrated me when I last used the shell was figuring out how to do all the things I was used to doing with dir in cmd.exe. Things like dir /o:d to order by date, or dir /a:r to show me all the read-only files seemed to take a huge amount of typing in Monad.

It can still take a bit of typing in PowerShell but last night I decided to make a little table that translated common actions in cmd.exe’s dir to PowerShell’s get-childitem. There are two lines for each entry in the table. One is the “full” version without use of parameter shortening or use of built-in aliases, and the second is the shortest I could make the command using the aliases that ship with PowerShell. My one concession: even in the full version I never use get-childitem. I always use “dir.”

cmd.exe PowerShell
dir dir
dir /s dir -recurse
  dir -r
dir /s *.txt dir -recurse -include *.txt
  dir -r -i *.txt
dir /s %temp%\*.txt   dir -recurse -include *.txt $env:temp
  dir -r -i *.txt $env:temp
dir /o:-d dir | sort-object -descending {$_.LastWriteTime}
  dir | sort -des LastWriteTime
dir /a:d dir | where-object {$_.PSIsContainer}
  dir | ? {$_.PSIsContainer}
dir /a:-d dir | where-object {$_.PSIsContainer}
  dir | ? {!$_.PSIsContainer}
dir /a:d /o:-d dir | where-object {$_.PSIsContainer} | sort -descending {$_.LastWriteTime}
  dir | ? {$_.PSIsContainer} | sort -des LastWriteTime
dir /a:hd dir -force | where-object {$_.Attributes -like ‘*Hidden*’ -and $_.PSIsContainer}
  dir -fo | ? {$_.Attributes -like ‘*H*’ -and $_.PSIsContainer}
dir /b dir | format-table Name -hideTableHeaders
  dir | ft Name -h

 

One nice thing about functions in PowerShell: / and - are legal in function names. So, in my profile, I’ve defined functions with names like dir/o-d and dir/s to get back to the short commands I’m used to.

Coming soon: a list of things you can do with get-childitem in PowerShell that aren’t possible with dir in cmd.exe.

Tags: , , ,

Posted in PowerShell, Programming | 7 Comments »