A few weeks ago I was getting Mark Russinovich up and running on PowerShell. While he liked the power the new model afforded him, he was frustrated because a bunch of things that were simple in CMD.EXE where now either hard or not available. I forget what the specific was but I think he wanted to know how to do the CMD.EXE equivalent of "dir /q". This command shows the owners of the files. Here is the output from a CMD.EXE session:
C:\Documents and Settings\jsnover.ntdev>dir /q Volume in drive C is PowerShell Rocks Volume Serial Number is 14B2-8C76 Directory of C:\Documents and Settings\jsnover.ntdev05/10/2007 07:57 AM <DIR> BUILTIN\Administrators .05/10/2007 07:57 AM <DIR> BUILTIN\Administrators ..04/16/2006 10:26 AM <DIR> NTDEV\jsnover .freemind06/18/2006 07:54 PM 4,608 NTDEV\jsnover custom11/05/2005 05:34 PM 24,498 NTDEV\jsnover err.txt11/05/2005 05:36 PM 49,283 NTDEV\jsnover err1.txt11/05/2005 05:37 PM 923 NTDEV\jsnover err2.txt01/21/2007 01:45 PM <DIR> NTDEV\jsnover Favorites06/16/2006 04:26 PM 6,219 NTDEV\jsnover foo05/21/2007 11:31 AM 7,340,032 BUILTIN\Administrators NTUSER.DAT06/30/2006 10:30 PM <DIR> NTDEV\jsnover PowerShell05/10/2007 08:00 AM <DIR> NTDEV\jsnover SecurityScans01/19/2006 03:28 PM <DIR> NTDEV\jsnover Start Menu11/05/2005 05:50 PM 213 NTDEV\jsnover t.msh05/27/2006 08:25 AM 13 NTDEV\jsnover t.txt06/14/2006 06:18 PM 19 NTDEV\jsnover t1.bat09/21/2006 07:33 PM 10,678 NTDEV\jsnover t1.txt06/15/2001 05:42 AM 4,081 NTDEV\jsnover testroot.cer01/19/2006 03:36 PM <DIR> NTDEV\jsnover Tracing02/06/2006 09:41 PM <DIR> NTDEV\jsnover WINDOWS 11 File(s) 7,440,567 bytes 9 Dir(s) 16,137,601,024 bytes free
Doing the same in PowerShell is quite difficult because the underlying .NET objects do not make this simple.
Out of the box, we provide an alias DIR which maps to Get-ChildItem and that works fine for those of use that usually just type "dir" but it doesn't work well for those people that want to use the "native" parameters for DIR (e.g. "dir /q").
There are a few commands that fall into the same category and we've been thinking about whether we should provide better DOS emulations for these. The thinking was that maybe we should get rid of the alias and write a function which supports the CMD.exe syntax and then does mostly the same thing. We brainstormed that for a while and we didn't like the idea of spending a lot of time on a path that in the end, we wanted people to migrate off of. If we spend calories on that, that will rob you of other great features (and we have a BUCKETLOAD of great features we want to deliver for the next release).
We thought maybe we should provide a function "Get-DosOptions" which would work like UNIX getopts() but work against a DOS syntax. We'd provide this and then you could use it to write a front end function to those CMD.EXE commands that you miss the most. I decided to ask Bruce whether this was feasible and he reminded me that this was simple to do in PowerShell and we were thinking about it all wrong.
So here it goes, I'm going to show you how to get the native CMD.EXE semantics for DIR. You can use the same technique to get the native semantics for most CMD.EXE commands. The first thing you'll want to do is to remove any alias that we've set up for you:
PS> Remove-Item Alias:dirPS>
Now define a function as follows and then run it:
PS> function dir {cmd /c dir $args}PS> dir /ad /q Volume in drive C is PowerShell Rocks Volume Serial Number is 14B2-8C76 Directory of C:\Documents and Settings\jsnover.ntdev05/10/2007 07:57 AM <DIR> BUILTIN\Administrators .05/10/2007 07:57 AM <DIR> BUILTIN\Administrators ..04/16/2006 10:26 AM <DIR> NTDEV\jsnover .freemind05/15/2007 10:56 PM <DIR> NTDEV\jsnover Application Data05/21/2007 07:53 PM <DIR> NTDEV\jsnover Cookies01/21/2007 01:45 PM <DIR> NTDEV\jsnover Favorites11/09/2005 05:00 PM <DIR> NTDEV\jsnover Local Settings06/06/2006 12:54 PM <DIR> NTDEV\jsnover NetHood06/30/2006 10:30 PM <DIR> NTDEV\jsnover PowerShell08/10/2004 10:57 AM <DIR> NTDEV\jsnover PrintHood05/21/2007 07:37 PM <DIR> NTDEV\jsnover Recent05/10/2007 08:00 AM <DIR> NTDEV\jsnover SecurityScans09/30/2006 08:26 PM <DIR> NTDEV\jsnover SendTo01/19/2006 03:28 PM <DIR> NTDEV\jsnover Start Menu06/11/2006 04:21 PM <DIR> NTDEV\jsnover Templates01/19/2006 03:36 PM <DIR> NTDEV\jsnover Tracing10/29/2005 10:01 PM <DIR> NTDEV\jsnover UserData02/06/2006 09:41 PM <DIR> NTDEV\jsnover WINDOWS 0 File(s) 0 bytes 18 Dir(s) 16,440,205,312 bytes free
You can do this for all your CMD.EXE favorites:
PS> function assoc {cmd /c assoc $args}PS> assoc .ps1.ps1=Microsoft.PowerShellScript.1PS> function vol {cmd /c vol $args}PS> vol /?Displays the disk volume label and serial number, if they exist.VOL [drive:]PS> vol c: Volume in drive C is PowerShell Rocks Volume Serial Number is 14B2-8C76PS>
You gotta love it! Enjoy!
Jeffrey Snover [MSFT]Windows Management Partner ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx