{"id":64,"date":"2006-11-24T21:19:37","date_gmt":"2006-11-24T21:19:37","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=64"},"modified":"2006-11-26T23:53:57","modified_gmt":"2006-11-26T23:53:57","slug":"windows-command-line-gets-smarter","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=64","title":{"rendered":"Windows Command Line has got smarter"},"content":{"rendered":"<p>Most die-hard command-line fans tend to agree that the Windows Command Prompt isn&#8217;t great. When you consider how old it is, it isn&#8217;t all that powerful. This isn&#8217;t the end of the world, as there is always <a title=\"cygwin download and documentation\" target=\"_blank\" href=\"http:\/\/www.cygwin.com\/\">cygwin<\/a>. <\/p>\n<p>But now Microsoft have released their new command prompt <a href=\"http:\/\/www.microsoft.com\/windowsserver2003\/technologies\/management\/powershell\/default.mspx\" title=\"Microsoft Powershell\">PowerShell<\/a>. I tried downloading it this evening, and so far, I really like it. <\/p>\n<p><!--more-->I heard of PowerShell last year when <a href=\"http:\/\/arstechnica.com\/guides\/other\/msh.ars\" title=\"Ars Technica guided tour\" target=\"_blank\">Ars Technica published a walkthrough on it<\/a> while it was still in beta. But I hadn&#8217;t got round to giving it a try. Actually, despite it&#8217;s age, the Ars Technica guide is still the best introduction I&#8217;ve found, so if you want to know more about PowerShell is all about, it&#8217;s worth a look.<\/p>\n<p>Some of my favourite features so far:<br \/>\n&#8212;&#8212;<br \/>\n<strong>Access the Registry in the same way as files<\/strong><\/p>\n<pre><code>PS C:\\\\>\r\nPS C:\\\\> Get-PSDrive\r\n\r\nName       Provider      Root                                   CurrentLocation\r\n----       --------      ----                                   ---------------\r\nAlias      Alias\r\nC          FileSystem    C:\\\\\r\ncert       Certificate   \\\\\r\nEnv        Environment\r\nFunction   Function\r\nHKCU       Registry      HKEY_CURRENT_USER\r\nHKLM       Registry      HKEY_LOCAL_MACHINE\r\nVariable   Variable\r\nW          FileSystem    W:\\\\\r\nX          FileSystem    X:\\\\\r\nY          FileSystem    Y:\\\\\r\nZ          FileSystem    Z:\\\\\r\n\r\n\r\nPS C:\\\\> cd HKLM:\r\nPS HKLM:\\\\> cd SOFTWARE\\\\IBM\\\\MQSeries\\\\CurrentVersion\\\\Configuration\\\\\r\nPS HKLM:\\\\SOFTWARE\\\\IBM\\\\MQSeries\\\\CurrentVersion\\\\Configuration> dir\r\n\r\n\r\n   Hive: Microsoft.PowerShell.Core\\\\Registry::HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\IBM\\\\MQ\r\nSeries\\\\CurrentVersion\\\\Configuration\r\n\r\nSKC  VC Name                           Property\r\n---  -- ----                           --------\r\n  0   1 ACPI                           {DoDialog}\r\n  0   1 AlertMonitor                   {Startup}\r\n  0   1 AllQueueManagers               {DefaultPrefix}\r\n  0   1 ClientExitPath                 {ExitsDefaultPath}\r\n  0   4 DefaultConfiguration           {V2_MachineName, V2_MQQMName, V2_Mach...\r\n  0   6 LogDefaults                    {LogDefaultPath, LogPrimaryFiles, Log...\r\n  3   0 QueueManager                   {}\r\n  4   0 Services                       {}\r\n  0   0 Trace                          {}\r\n  0   1 WebAdministration              {Trace}\r\n<\/code><\/pre>\n<p>You can also access system certificates, environment variables, and more in the same way. Very cool!<br \/>\n&#8212;&#8212;<br \/>\n<strong>SQL-type syntax<\/strong><br \/>\nNo more fighting with awk to get bits that I want out of command output! Instead of returning strings, PowerShell is object-oriented, and you can run a variety of commands with the returned objects. <\/p>\n<p>For example&#8230;<\/p>\n<pre><code>PS C:\\\\MyFolder\\\\src> Get-ChildItem | select name\r\n\r\nName\r\n----\r\namqssl.h\r\namqsslchk.c\r\namqsslchk.exe\r\namqsslcon.c\r\namqsslenv.c\r\namqsslerr.c\r\namqsslgsk.c\r\namqsslikm.c\r\namqsslpcf.c\r\namqsslper.c\r\namqsslutl.c\r\namqsslwmq.c\r\ncompile.bat\r\ncompile_with_ikeyman.bat\r\nlinux_compile.sh\r\nsolaris_build.sh\r\n\r\nPS C:\\\\MyFolder\\\\src> Get-ChildItem | select name, extension\r\n\r\nName                                    Extension\r\n----                                    ---------\r\namqssl.h                                .h\r\namqsslchk.c                             .c\r\namqsslchk.exe                           .exe\r\namqsslcon.c                             .c\r\namqsslenv.c                             .c\r\namqsslerr.c                             .c\r\namqsslgsk.c                             .c\r\namqsslikm.c                             .c\r\namqsslpcf.c                             .c\r\namqsslper.c                             .c\r\namqsslutl.c                             .c\r\namqsslwmq.c                             .c\r\ncompile.bat                             .bat\r\ncompile_with_ikeyman.bat                .bat\r\nlinux_compile.sh                        .sh\r\nsolaris_build.sh                        .sh\r\n\r\nPS C:\\\\MyFolder\\\\src> Get-ChildItem | select name, extension | sort-object extension\r\n\r\nName                                    Extension\r\n----                                    ---------\r\ncompile_with_ikeyman.bat                .bat\r\ncompile.bat                             .bat\r\namqsslper.c                             .c\r\namqsslpcf.c                             .c\r\namqsslwmq.c                             .c\r\namqsslutl.c                             .c\r\namqsslenv.c                             .c\r\namqsslcon.c                             .c\r\namqsslchk.c                             .c\r\namqsslikm.c                             .c\r\namqsslgsk.c                             .c\r\namqsslerr.c                             .c\r\namqsslchk.exe                           .exe\r\namqssl.h                                .h\r\nsolaris_build.sh                        .sh\r\nlinux_compile.sh                        .sh\r\n<\/code><\/pre>\n<p>There is lots more you can do, but if you&#8217;re familiar with the sort of FOR, WHILE, SELECT and so on in SQL, then you&#8217;ll be in familiar territory. It&#8217;s really nice.<br \/>\n&#8212;&#8212;<br \/>\n<strong>You can use .NET API<\/strong><br \/>\nThere are lots of nice bits of functionality in .NET, and now you can access them interactively at the command-line. For example:<\/p>\n<pre><code>PS C:\\\\> ([xml](new-object Net.WebClient).DownloadString(\"http:\/\/dalelane.co.\r\nuk\/blog\/?feed=rss2\")).rss.channel.item | select title\r\n\r\ntitle\r\n-----\r\nI can be a C-numptie\r\nMaintaining two blogs is difficult\r\nwii!\r\nThe differences between men and women\r\nwinter has arrived\r\nUsing Scheduled Tasks and batch files to do automatic backups\r\nYoung people make a massive contribution to society\r\nWhat IET members do in the evenings\r\nThe etch-a-sketch approach to legislation<\/code><\/pre>\n<p>You can use pretty much any function in a .NET library, so even GUIs can be created on-the-fly at the command-line:<\/p>\n<pre><code>PS C:\\\\> [Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\")\r\n\r\nGAC    Version        Location\r\n---    -------        --------\r\nTrue   v2.0.50727     C:\\\\WINDOWS\\\\assembly\\\\GAC_MSIL\\\\System.Windows.Forms\\\\2.0.0.0__b77a5c561934e08...\r\n\r\nPS C:\\\\> $window = new-object Windows.Forms.Form\r\nPS C:\\\\> $window.text = \"This is a Windows form!\"\r\nPS C:\\\\> $button = new-object Windows.Forms.Button\r\nPS C:\\\\> $button.text = \"Click me!\"\r\nPS C:\\\\> $window.controls.add($button)\r\nPS C:\\\\> $window.showDialog()<\/code><\/pre>\n<p>and up pops a window with a button on it!<br \/>\n&#8212;&#8212;<\/p>\n<p>I&#8217;ve still got more to learn, but I&#8217;ve already started to knock together some very powerful scripts. If you&#8217;ve ever found the Windows Command Prompt a bit limited, I&#8217;d recommend you <a title=\"Microsoft PowerShell\" target=\"_blank\" href=\"http:\/\/www.microsoft.com\/windowsserver2003\/technologies\/management\/powershell\/default.mspx\">download it and have a play<\/a>. <\/p>\n<p>Other PowerShell resources that I found useful are tagged on my <a href=\"http:\/\/del.icio.us\/dalelane\/powershell\" title=\"del.icio.us bookmarks\" target=\"resources I found useful about PowerShell\">del.icio.us bookmarks<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most die-hard command-line fans tend to agree that the Windows Command Prompt isn&#8217;t great. When you consider how old it is, it isn&#8217;t all that powerful. This isn&#8217;t the end of the world, as there is always cygwin. But now Microsoft have released their new command prompt PowerShell. I tried downloading it this evening, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}