{"id":3635,"date":"2016-03-09T20:38:35","date_gmt":"2016-03-09T20:38:35","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3635"},"modified":"2016-03-10T08:57:35","modified_gmt":"2016-03-10T08:57:35","slug":"finally-decided-to-learn-a-little-powershell","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/finally-decided-to-learn-a-little-powershell\/","title":{"rendered":"Finally decided to learn a little Powershell"},"content":{"rendered":"<p>For years I&#8217;ve been thinking of learning Powershell, but I&#8217;ve never really had a real need for it, so subsequently it got neglected for such a long time. <\/p>\n<p>I&#8217;ve finally decided to spend some time with it and see what it can do for me. Let&#8217;s start by looking at some of the basics to getting started with Powershell.<\/p>\n<p><strong>Commands (or Cmdlets)<\/strong><\/p>\n<p>Built-in commands are also known as Cmdlets, although I&#8217;ve also seen that some users prefer to think of Cmdlets as multiple commands &#8211; more like batch files. Either way, I&#8217;ll use the word interchangeably.<\/p>\n<p><em>Note: Commands are case-insensitive<\/em><\/p>\n<p><strong>Let&#8217;s jump right in<\/strong><\/p>\n<p>Let&#8217;s have a look at an example command in Powershell<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Process | where {$_.CPU -gt 1000}\r\n<\/pre>\n<p>In the above, the command is <em>Get-Process<\/em>. This returns a list of the current processes running on your computer. Running Get-Process will result in eight headings (on my machine). Get-Process actually returns data as objects which we then pipe using | to the where clause.<\/p>\n<p>The where clause syntax uses the braces {} to group together to form a block (much like blocks in C++ etc.). Within this block we use the $_ syntax which is a placeholder which in essence means &#8220;the current value&#8221; from the Get-Process command. In other words each item return from Get-Process is piped to the where clause and the $_ indicates the current value. From this we use standard object oriented style dot notation to get at a property on the item $_, in this case the property is CPU. <\/p>\n<p><em>Note: The $ is used for to denote a variable, whether it&#8217;s supplied by default by Powershell or created by the user<\/em><\/p>\n<p>Next up with have the -gt which as I&#8217;m sure you can determine, means &#8220;greater than&#8221; and ofcourse on the right hand side of this expression is the value we want to test for. <\/p>\n<p><strong>How do we determine what properties exist on an object?<\/strong><\/p>\n<p>In the previous command we used the CPU property of the results from Get-Process but how did we know that property existed and are there more properties we could have used? Ofcourse, there&#8217;s documentation which can tell us such things, but we can also use another Powershell command to tell us.<\/p>\n<p>By typing the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Process | Get-Member\r\n<\/pre>\n<p>We can pipe the Get-Process object into Get-Member which will output a list of properties (and other members) of the type returned by Get-Process. If you run this you&#8217;ll find the CPU property (and many others).<\/p>\n<p>Actually using the Powershell command prompt we can also view the properties by pressing ctrl+space after the dot, this allows us to list all available member names for an object. In the case of Get-Process this will list 122 possible member names.<\/p>\n<p><strong>Getting help<\/strong><\/p>\n<p>Powershell&#8217;s help system is very powerful. Simply type<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Help\r\n<\/pre>\n<p>to view the help on the Get-Help command. From here you&#8217;ll see you can type<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Help Get-Process\r\n<\/pre>\n<p>which will display help on the Get-Process command. We can also view examples for this command using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Help Get-Process -examples\r\n<\/pre>\n<p><strong>Aliases<\/strong><\/p>\n<p>Some commands are also aliased, meaning our original <em>Get-Process | where {$_.CPU -gt 1000}<\/em> can be rewritten as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nps | where {$_.CPU -gt 1000}\r\n<\/pre>\n<p>ps is simply an alias to the longer winded Get-Process and we can find an alias (if one exists) using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGet-Alias -definition Get-Process\r\n<\/pre>\n<p><strong>Ctrl+Space<\/strong><\/p>\n<p>I noted previously that you can get a list of member names by pressing the combination ctrl+space after the dot, this is also useful after the &#8211; operator, for example when looking for alternatives to gt we can type &#8211; then ctrl+space and a list of options appears which includes, bnot, eq, f and more. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>For years I&#8217;ve been thinking of learning Powershell, but I&#8217;ve never really had a real need for it, so subsequently it got neglected for such a long time. I&#8217;ve finally decided to spend some time with it and see what it can do for me. Let&#8217;s start by looking at some of the basics to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[118],"tags":[],"class_list":["post-3635","post","type-post","status-publish","format-standard","hentry","category-powershell"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3635"}],"version-history":[{"count":14,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3635\/revisions"}],"predecessor-version":[{"id":3676,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3635\/revisions\/3676"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}