CMD.exe compatibility

CMD.exe compatibility

  • Comments 18

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.ntdev

05/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 .freemind
06/18/2006 07:54 PM 4,608 NTDEV\jsnover custom
11/05/2005 05:34 PM 24,498 NTDEV\jsnover err.txt
11/05/2005 05:36 PM 49,283 NTDEV\jsnover err1.txt
11/05/2005 05:37 PM 923 NTDEV\jsnover err2.txt
01/21/2007 01:45 PM <DIR> NTDEV\jsnover Favorites
06/16/2006 04:26 PM 6,219 NTDEV\jsnover foo
05/21/2007 11:31 AM 7,340,032 BUILTIN\Administrators NTUSER.DAT
06/30/2006 10:30 PM <DIR> NTDEV\jsnover PowerShell
05/10/2007 08:00 AM <DIR> NTDEV\jsnover SecurityScans
01/19/2006 03:28 PM <DIR> NTDEV\jsnover Start Menu
11/05/2005 05:50 PM 213 NTDEV\jsnover t.msh
05/27/2006 08:25 AM 13 NTDEV\jsnover t.txt
06/14/2006 06:18 PM 19 NTDEV\jsnover t1.bat
09/21/2006 07:33 PM 10,678 NTDEV\jsnover t1.txt
06/15/2001 05:42 AM 4,081 NTDEV\jsnover testroot.cer
01/19/2006 03:36 PM <DIR> NTDEV\jsnover Tracing
02/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:dir
PS>

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.ntdev

05/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 .freemind
05/15/2007 10:56 PM <DIR> NTDEV\jsnover Application Data
05/21/2007 07:53 PM <DIR> NTDEV\jsnover Cookies
01/21/2007 01:45 PM <DIR> NTDEV\jsnover Favorites
11/09/2005 05:00 PM <DIR> NTDEV\jsnover Local Settings
06/06/2006 12:54 PM <DIR> NTDEV\jsnover NetHood
06/30/2006 10:30 PM <DIR> NTDEV\jsnover PowerShell
08/10/2004 10:57 AM <DIR> NTDEV\jsnover PrintHood
05/21/2007 07:37 PM <DIR> NTDEV\jsnover Recent
05/10/2007 08:00 AM <DIR> NTDEV\jsnover SecurityScans
09/30/2006 08:26 PM <DIR> NTDEV\jsnover SendTo
01/19/2006 03:28 PM <DIR> NTDEV\jsnover Start Menu
06/11/2006 04:21 PM <DIR> NTDEV\jsnover Templates
01/19/2006 03:36 PM <DIR> NTDEV\jsnover Tracing
10/29/2005 10:01 PM <DIR> NTDEV\jsnover UserData
02/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.1
PS> 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-8C76
PS>

You gotta love it! Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
  • Yes and no.  "Yes" because now I can use all the CMD DIR options that I'm used to.  "No" because it turns all the output into straight text, defeating the purpose of being in PowerShell.

    I agree with you - I don't want your team diverted from working on more important stuff.  But a community effort should be able to make a great "function dir" replacement for "alias dir".

  • This is a good idea.  What may be helpful especially to new PowerShell users is a set of functions that could add directly into their profile to do this sort of thing for the commonest DOS commands that are likely to be required

  • A compromise between old cmd style and the new PowerShell object orientation might be a function like this:

    function dir(cmd /c dir /b $args) | foreach{[System.IO.FileInfo](Convert-Path $_)}

    Now you can type dir /ah and one still gets FileInfo objects back. Wow, it's so easy to get only hidden files ;-)

    Don't get me wrong, I love PowerShell, but Get-Childitem is by far the hardest thing to learn in PowerShell.

  • if ($args -ne $null) { $all = $args } else { $all = $input }

    cmd /c $all

  • Maybe I am missing something here, but how about extending some of these existing commands, like Get-ChildItem, to handle those DOS-like items? Perhaps the new parameter names would be more PowerShell like (that is, not /q, but something more meaningful).

    Now, I know that Get-ChildItem can be used for things other than the file system, but some of the other information stores that Get-ChildItem can query also have owners too (in this case), like Get-ChildItem HKCU:\.  Where the parameter does not make sense like Get-ChildItem Env:\, the parameter could either be ignored or an appropriate error issued.

    I have probably just over-simplified this whole process, but I thought I would pitch my idea none the less.

  • > Maybe I am missing something here, but how about extending some of these existing commands, like Get-ChildItem, to handle those DOS-like items?

    We can and it is on the list of things to do.  That said, here is the reality of how these things work.  We keep lists of all the things that we want to do for a release.  That list comes from us, our partners, our customers etc.  Then we decide upon the THEMEs for a release.  Think of this as "What will the newspaper say about the next release?".  We then prioritize the list of things we want to do based upon how they support those themes.  After that, we look at the input from customers and figure out how we can address as much of it as possible.  There is no fixed algorithm for how teams do this and frankly we've tried a few approaches (dedicate a milestone to this, let people do it catch-as-catch can, etc) and we're going to continue to experiment because we value responding to customers highly.  All that said, no matter what we do, there will be things that don't make the cut line for a release.  "To ship is to choose."

    So will we try to get this done? Definately.

    Will it get done?  I don't know.

    This should point out how important it is for the community to speak up and let us know what is important to you.  We look at every request that comes in.  Common sense will tell you that if we see 100 people asking for feature 1 and 5 people asking for feature 2, feature 1 has a higher probably of getting done.  Ergo - even though you know that something has been requested, you should request it or vote for that item.

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • Thanks to Mark Russinovich for getting you guys' to think this through. And so simple... :)

    Frisky

  • This kind of issue has left me dissapointed with PowerShell's performance thus far as an interactive shell.  CMD and csh commands have option and formatting choices that have been refined over many years.  I don't want to have to rewrite these myself.  And while it's cool that I can just use the old DIR inside powershell, what I really want is a new dir with all the formatting and options of the old dir, along with the object features that let it be leveraged in the powershell environment.  

    I'd love to see a default compatibility pack providing CMD like formatting and options, as well as optional packs mimicking other popular shells.

  • The PowerShell Community Extensions project provides functions that provide some of the features you might be looking for:

    $content = Get-Content "c:/Program Files/PowerShell Community Extensions/Profile/Dir.ps1"

    $xmlContent = $("<XML>"; ($content -match "###") -replace "###",""; "</XML>" | Out-String)

    $xml = [xml] $xmlContent

    $xml.XML.Function | Format-List Name,Description

    name        : dirx

    Description :

                  DIRX is an extended dir function that is used by dird, dirs and dirt.

                  It provides shortcuts for displaying files/dirs with particular

                  attributes.  For more information execute "dirx -?".

    name        : dird

    Description :

                  Displays just containers (directories).

    name        : dirw

    Description :

                  Displays folder contents in wide format.

    name        : dirs

    Description :

                  Displays folder contents sorted by size descending.

    name        : dirt

    Description :

                  Displays folder contents sorted by time descending.

     

    Lee

  • This same trick works great for accessing classic UNIX commands that run under SFU/SUA/Interix.

    #aliasing functions for SUA unix utilities

    function dig { & "$env:systemroot\posix.exe" /u /c /bin/dig $args }

    #By invoking xterm via /bin/sh we get an environment set up before xterm starts (good for xterm -e ssh foo)

    #function xterm { & "$env:systemroot\psxrun.exe" -u -p /usr/local/bin/xterm -- xterm $args }

    function xterm { & "$env:systemroot\psxrun.exe" -u /bin/sh -l -c "/usr/local/bin/xterm $args" }

    For statically linked PSXSS binaries you can also hard-link them to /common where foo links to foo.exe.

    For example whois could be a function:

    function dig { & "$env:systemroot\posix.exe" /u /c /usr/local/bin/whois $args }

    -or-

    % ln /usr/local/bin/whois /common/whois.exe

    Since C:\SFU or C:\Windows\SUA is automatically put into the PATH environmental variable when SFU or SUA respectively is installed the hard link option gets you a two-fer: it works in CMD.EXE and PowerShell.

  • Could you make a blog entry about why the copy function of the PS window imitates that of CMD in that the content is stays wrapped in the copy.

    That's what annoys about cmd the most really and it's in PS too, run some command like say tracert and use the mouse/quickedit to copy it then paste to somewhere and it looks like this:

     8    12 ms    13 ms    13 ms  gi3-3.ar1.ARN3.gblx.net [67.

    17.196.241]

     9   105 ms   105 ms   105 ms  qwest-1.ar3.JFK1.gblx.net [2

    08.50.13.170]

    10   108 ms   107 ms   107 ms  jfk-core-02.inet.qwest.net [

    205.171.30.57]

    11   123 ms   122 ms   123 ms  atl-core-02.inet.qwest.net [

    67.14.14.14]

    What's the usability scenario where this behaviour is preferred over the other? Usually Microsoft is good in getting things right for the common scenario and not optimizing for the edge/corner cases? I know you can pipe output to file and so on but it's just way too often that I'd actually want to copy something already on the screen that this issue should simply go on being ignored.

  • Great! But there are a lot of options and commands to create wrapper-funcions. I gave an example of this some months ago, when I wrote about DIR /AD command and its Powershell replacement, the ugly get-childItem | ? {$_.PSIsContainer}

    []s,

    --

    Vinicius Canto <scripterbr_at_gmail_dot_com>

    MVP Visual Developer - Scripting

    MCP Windows 2000 Server, Windows XP e SQL Server 2000

    Blog sobre Scripting: http://viniciuscanto.blogspot.com

  • I'm curious about how the old Doskey macro utility interacts with cmd.exe and the win32 console.

    It's my understanding that in NT based systems, Doskey is merely an interface for accessing functionality built into the win32 console. But if that is the case, why don't certain funcitons work in powershell?

    For example, the command doskey /history prints out the command buffer in cmd, but not in psh. Yet, creating a macro using the switch /exename=powershell.exe will work even to the extent of overriding psh aliases.

    My question is: are the doskey macros handled by the console itself and, if so, would there be an easy way to migrate them to psh aliases?

  • I use the DIR switches all the time.  I have to confess that I take the lazy way and use

    cmd /c dir /a-d

    when in PowerShell.  I like to type as few characters as possible.

  • hi ...

    do tink about this problem in mi sistem..?

    this comand : assoc .exe=.exe

    thank you..

    ithoo.kish@yahoo.com

Page 1 of 2 (18 items) 12