{"id":65,"date":"2018-01-04T10:33:26","date_gmt":"2018-01-04T18:33:26","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/koryt\/?p=65"},"modified":"2020-04-29T09:53:59","modified_gmt":"2020-04-29T16:53:59","slug":"powershell-for-programmers-basic-syntax-cmdlets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-basic-syntax-cmdlets\/","title":{"rendered":"PowerShell For Programmers: Basic Syntax &#8212; CMDLETs"},"content":{"rendered":"<p>PowerShell might look really strange to you. Many people assume PowerShell is basically CMD-prompt 2.0 because of the way it looks, but it really is a fully operational scripting language underneath.<\/p>\n<p>PowerShell has something called a cmdlet (command-let), which for the most part is the same idea as functions you&#8217;re used to from other languages.<\/p>\n<p>I&#8217;ll assume you&#8217;re coming from a more classic language and you&#8217;re used to calling functions like this:<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:0465e1b7-f9ed-4d96-8687-bd12a3a13280\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">MyFunction(Value, Value, Value)\r\n\r\n<\/pre>\n<\/div>\n<p>If you&#8217;ve been using cmdlets in PowerShell like this, <span style=\"text-decoration: underline;\"><strong>you&#8217;ve been doing it wrong<\/strong><\/span>. This could cause you some serious problems down the line.<\/p>\n<blockquote><p><em>&#8220;But I can type Get-Service(&#8220;alg&#8221;) and it works just fine!&#8221; <\/em><\/p><\/blockquote>\n<p>Probably, but that&#8217;s just PowerShell doing some nice parsing tricks to help you. This will not work with every cmdlet or every parameter set (overload) of those cmdlets.<\/p>\n<p>We should actually type something like: <em>Get-Service -Name &#8220;Alg&#8221;<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>There are a lot of nice little tricks and shortcuts we can use to shorten lines as well, and I&#8217;ll point some common ones out as we go.<\/p>\n<p>Here are some basics you should know about cmdlets:<\/p>\n<ul>\n<li>POWERSHELL IS NOT CASE SENSITIVE: <em>Get-Command<\/em>, <em>get-command<\/em> and <em>GeT-CoMmaNd<\/em> are the same<\/li>\n<li>Cmdlets are always of the form Verb-Noun to help understand what they do without having to look at help data<\/li>\n<li>PowerShell calls its arguments &#8220;Parameters&#8221;<\/li>\n<li>Instead of just providing the <em>parameter values<\/em> in a specified order, we need to also provide the <em>parameter name<\/em> those values are going to. This makes them <em>order-independent<\/em><\/li>\n<li>Parameter names always start with a dash like -Name, -ComputerName, -Force<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:39c06e38-cfe1-4681-b4ef-3f178d2717ed\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">#these will work the same, despite the order difference\r\nget-service -name \"ALG\" -ComputerName \"localhost\"\r\nget-service -ComputerName \"localhost\" -name \"ALG\"\r\n<\/pre>\n<\/div>\n<ul>\n<li>Some parameters have no value and are called switch parameters (like -force, -verbose, etc) these are settings that default to off and just typing -Force would switch them on.<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:d97e3650-3045-494d-9a7b-4535c622d1ca\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">#this will fail if notepad isn't open\r\nstop-process -name notepad -whatif\r\n<\/pre>\n<\/div>\n<ul>\n<li>Some parameters are set to be &#8220;Positional&#8221;, these let us eschew writing the <i>parameter name<\/i> (-ComputerName, -Name, -Whatever) and just provide the values. In this case the order matters, but you can always explicitly name them instead. I\u2019ll show you how to identify these in the syntax later, but if you see people doing this, recognize that it is just a shortcut.<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:40c8afc4-857c-4191-8f16-4432d5ca0ac5\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">#these are the same\r\nget-service -name \"alg\"\r\nget-service \"ALG\"\r\n<\/pre>\n<\/div>\n<ul>\n<li>Another common shortcut is that PowerShell can sometimes assume something you type is a string, even if you forget the quotes. A good rule of thumb is that you can assume a parameter expecting string data will let you skip the quotes (as long as you don\u2019t have spaces or weird special characters). If PowerShell yells at you, just throw quotes on, but it explains why you usually see people type stuff like this:<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:2cdfd97b-d70f-4746-aeda-a1a70d59cb59\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">#no quotes + positional parameter = short line\r\nget-service alg\r\n<\/pre>\n<\/div>\n<ul>\n<li>To see the syntax for a cmdlet you can use technet documentation or look in the shell using another cmdlet:<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:326e2f33-6bf4-4d57-bc45-07d1a16f4cf5\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">get-command -name \"get-service\" -Syntax\r\n<\/pre>\n<\/div>\n<ul>\n<li>If you run this command on your own you will see multiple syntax blocks come back with distinct parameters. These are <i>parameter sets<\/i>, which you can think of as <i>overloads<\/i>.<\/li>\n<li>To see more detailed info + examples + explanations of parameters we can use the help cmdlet. When using it, use the switch parameter -ShowWindow to make your life easier.<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:82f72d75-c35a-4f76-9162-1dbb5cb0d00d\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">Get-help Get-Service -showWindow\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86513\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/01\/Capture_thumb.png\" alt=\"\" width=\"855\" height=\"522\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/01\/Capture_thumb.png 855w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/01\/Capture_thumb-300x183.png 300w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/01\/Capture_thumb-768x469.png 768w\" sizes=\"(max-width: 855px) 100vw, 855px\" \/><\/p>\n<p>If you followed along with those points, then that is all you <b>need <\/b>to know about cmdlets.<\/p>\n<ul>\n<li>Recognize that the long-hand is the way you should try to write most stuff for readability, but the shortcuts are common and valuable<\/li>\n<li>If you are doing things like MyFunction(value,value) then you should stop and get on the correct syntax bandwagon to save yourself issues.<\/li>\n<\/ul>\n<p>There are a couple other notes worth mentioning in regard to cmdlets and basic PowerShell syntax:<\/p>\n<ul>\n<li>PowerShell contains <i>Aliases<\/i> that are alternate names for cmdlets. Many of these exist to make adoption of the language easier. They will either be anchored back to other shell commands (CMD, terminal, etc) or other programming language paradigms. Here are a couple common ones that you have probably used or seen used:\n<ul>\n<li>Things like Dir are well known from CMD, but Get-Childitem is unintuitive so they added the alias.<\/li>\n<li>Aliases can take in the parameters the same way as their CMDLETs:<\/li>\n<li>As a best practice, try to use full cmdlet names in your scripts because not everyone knows aliases. However, for certain ones like Dir, CD, etc it might be easier to understand than the real cmdlet. Use your best judgement.<\/li>\n<li>If you ever see something that doesn&#8217;t look like VERB-NOUN it is likely an alias, which you can look at with get-command or get-alias<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:53313763-ea4d-46cf-95fe-0d69b6012dc7\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:ps decode:true \">#all do the same thing\r\nGet-ChildItem -Path c:\\\r\ndir -Path c:\\\r\nls -Path c:\\\r\ngci -Path c:\\\r\n<\/pre>\n<\/div>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:3545f248-b019-45d7-afc3-96f76bc83cfa\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; padding: 0px; float: none;\">\n<pre class=\"lang:default decode:true \">get-command dir\r\n\r\nCommandType Name Version Source\r\n----------- ---- ------- ------\r\nAlias dir -&amp;gt; Get-ChildItem\r\n\r\n<\/pre>\n<\/div>\n<ul>\n<li>External commands (all the old cmd stuff) like icacls, ipconfig, etc. all live on the PowerShell environment path. These spin up a new process when called, but you can use them in your script if needed. Try not to as they have way less standardized syntax than true cmdlets.<\/li>\n<\/ul>\n<p>Well that\u2019s all for now, hopefully this helps you get going on your PowerShell journey and clear up why you might see so many variations of the same command. I\u2019ll be adding to the series to cover other quirks as well so let me know in the comments if there is any particular topics confusing you!<\/p>\n<p>If you find this helpful don&#8217;t forget to rate, comment and share\u00a0\ud83d\ude42<\/p>\n<p>For the main series post, check back <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-a-quick-start-guide\/\">here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell might look really strange to you. Many people assume PowerShell is basically CMD-prompt 2.0 because of the way it looks, but it really is a fully operational scripting language underneath. PowerShell has something called a cmdlet (command-let), which for the most part is the same idea as functions you&#8217;re used to from other languages. [&hellip;]<\/p>\n","protected":false},"author":7300,"featured_media":87096,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1738,2126],"tags":[2221,2125],"class_list":["post-65","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-powershell_for_programmers","tag-kory-thacher","tag-koryt"],"acf":[],"blog_post_summary":"<p>PowerShell might look really strange to you. Many people assume PowerShell is basically CMD-prompt 2.0 because of the way it looks, but it really is a fully operational scripting language underneath. PowerShell has something called a cmdlet (command-let), which for the most part is the same idea as functions you&#8217;re used to from other languages. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/7300"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}