{"id":17712,"date":"2007-05-21T13:53:26","date_gmt":"2007-05-21T21:53:26","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17712"},"modified":"2020-07-15T23:59:01","modified_gmt":"2020-07-16T07:59:01","slug":"cmd-exe-compatibility","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/cmd-exe-compatibility\/","title":{"rendered":"CMD.exe compatibility"},"content":{"rendered":"<p>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 &#8220;dir \/q&#8221;. This command shows the owners of the files. Here is the output from a CMD.EXE session:<\/p>\n<pre class=\"prettyprint\">C:\\Documents and Settings\\jsnover.ntdev&#62;dir \/q\r\nVolume in drive C is PowerShell Rocks\r\nVolume Serial Number is 14B2-8C76\r\n\r\nDirectory of C:\\Documents and Settings\\jsnover.ntdev\r\n\r\n05\/10\/2007 07:57 AM &#60;DIR&#62; BUILTIN\\Administrators .\r\n05\/10\/2007 07:57 AM &#60;DIR&#62; BUILTIN\\Administrators ..\r\n04\/16\/2006 10:26 AM &#60;DIR&#62; NTDEV\\jsnover .freemind\r\n06\/18\/2006 07:54 PM 4,608 NTDEV\\jsnover custom\r\n11\/05\/2005 05:34 PM 24,498 NTDEV\\jsnover err.txt\r\n11\/05\/2005 05:36 PM 49,283 NTDEV\\jsnover err1.txt\r\n11\/05\/2005 05:37 PM 923 NTDEV\\jsnover err2.txt\r\n01\/21\/2007 01:45 PM &#60;DIR&#62; NTDEV\\jsnover Favorites\r\n06\/16\/2006 04:26 PM 6,219 NTDEV\\jsnover foo\r\n05\/21\/2007 11:31 AM 7,340,032 BUILTIN\\Administrators NTUSER.DAT\r\n06\/30\/2006 10:30 PM &#60;DIR&#62; NTDEV\\jsnover PowerShell\r\n05\/10\/2007 08:00 AM &#60;DIR&#62; NTDEV\\jsnover SecurityScans\r\n01\/19\/2006 03:28 PM &#60;DIR&#62; NTDEV\\jsnover Start Menu\r\n11\/05\/2005 05:50 PM 213 NTDEV\\jsnover t.msh\r\n05\/27\/2006 08:25 AM 13 NTDEV\\jsnover t.txt\r\n06\/14\/2006 06:18 PM 19 NTDEV\\jsnover t1.bat\r\n09\/21\/2006 07:33 PM 10,678 NTDEV\\jsnover t1.txt\r\n06\/15\/2001 05:42 AM 4,081 NTDEV\\jsnover testroot.cer\r\n01\/19\/2006 03:36 PM &#60;DIR&#62; NTDEV\\jsnover Tracing\r\n02\/06\/2006 09:41 PM &#60;DIR&#62; NTDEV\\jsnover WINDOWS\r\n11 File(s) 7,440,567 bytes\r\n9 Dir(s) 16,137,601,024 bytes free<\/pre>\n<p>Doing the same in PowerShell is quite difficult because the underlying .NET objects do not make this simple.\nOut 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 &#8220;dir&#8221; but it doesn&#8217;t work well for those people that want to use the &#8220;native&#8221; parameters for DIR (e.g. &#8220;dir \/q&#8221;).\nThere are a few commands that fall into the same category and we&#8217;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&#8217;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).\nWe thought maybe we should provide a function &#8220;Get-DosOptions&#8221; which would work like UNIX getopts() but work against a DOS syntax. We&#8217;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.\nSo here it goes, I&#8217;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&#8217;ll want to do is to remove any alias that we&#8217;ve set up for you:<\/p>\n<pre class=\"prettyprint\">PS&#62; Remove-Item Alias:dir\r\nPS&#62;\r\nNow define a function as follows and then run it:\r\nPS&#62; function dir {cmd \/c dir $args}\r\nPS&#62; dir \/ad \/q\r\nVolume in drive C is PowerShell Rocks\r\nVolume Serial Number is 14B2-8C76\r\n\r\nDirectory of C:\\Documents and Settings\\jsnover.ntdev\r\n\r\n05\/10\/2007 07:57 AM &#60;DIR&#62; BUILTIN\\Administrators .\r\n05\/10\/2007 07:57 AM &#60;DIR&#62; BUILTIN\\Administrators ..\r\n04\/16\/2006 10:26 AM &#60;DIR&#62; NTDEV\\jsnover .freemind\r\n05\/15\/2007 10:56 PM &#60;DIR&#62; NTDEV\\jsnover Application Data\r\n05\/21\/2007 07:53 PM &#60;DIR&#62; NTDEV\\jsnover Cookies\r\n01\/21\/2007 01:45 PM &#60;DIR&#62; NTDEV\\jsnover Favorites\r\n11\/09\/2005 05:00 PM &#60;DIR&#62; NTDEV\\jsnover Local Settings\r\n06\/06\/2006 12:54 PM &#60;DIR&#62; NTDEV\\jsnover NetHood\r\n06\/30\/2006 10:30 PM &#60;DIR&#62; NTDEV\\jsnover PowerShell\r\n08\/10\/2004 10:57 AM &#60;DIR&#62; NTDEV\\jsnover PrintHood\r\n05\/21\/2007 07:37 PM &#60;DIR&#62; NTDEV\\jsnover Recent\r\n05\/10\/2007 08:00 AM &#60;DIR&#62; NTDEV\\jsnover SecurityScans\r\n09\/30\/2006 08:26 PM &#60;DIR&#62; NTDEV\\jsnover SendTo\r\n01\/19\/2006 03:28 PM &#60;DIR&#62; NTDEV\\jsnover Start Menu\r\n06\/11\/2006 04:21 PM &#60;DIR&#62; NTDEV\\jsnover Templates\r\n01\/19\/2006 03:36 PM &#60;DIR&#62; NTDEV\\jsnover Tracing\r\n10\/29\/2005 10:01 PM &#60;DIR&#62; NTDEV\\jsnover UserData\r\n02\/06\/2006 09:41 PM &#60;DIR&#62; NTDEV\\jsnover WINDOWS\r\n0 File(s) 0 bytes\r\n18 Dir(s) 16,440,205,312 bytes free<\/pre>\n<p>You can do this for all your CMD.EXE favorites:<\/p>\n<pre class=\"prettyprint\">PS&#62; function assoc {cmd \/c assoc $args}\r\nPS&#62; assoc .ps1\r\n.ps1=Microsoft.PowerShellScript.1\r\nPS&#62; function vol {cmd \/c vol $args}\r\nPS&#62; vol \/?\r\nDisplays the disk volume label and serial number, if they exist.\r\n\r\nVOL [drive:]\r\nPS&#62; vol c:\r\nVolume in drive C is PowerShell Rocks\r\nVolume Serial Number is 14B2-8C76\r\nPS&#62;<\/pre>\n<p>You gotta love it! Enjoy!\nJeffrey Snover [MSFT]\nWindows Management Partner Architect\nVisit the Windows PowerShell Team blog at: http:\/\/blogs.msdn.com\/PowerShell\nVisit the Windows PowerShell ScriptCenter at: http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":685,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17712","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/685"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=17712"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17712\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=17712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}