{"id":857,"date":"2020-11-21T14:03:35","date_gmt":"2020-11-21T20:03:35","guid":{"rendered":"http:\/\/ninmonkeys.com\/blog\/?p=857"},"modified":"2021-01-09T21:17:29","modified_gmt":"2021-01-10T03:17:29","slug":"learning-powershell","status":"publish","type":"post","link":"https:\/\/ninmonkeys.com\/blog\/2020\/11\/21\/learning-powershell\/","title":{"rendered":"Learning PowerShell"},"content":{"rendered":"\n\n\n<p><\/p>\n\n\n\n<h2>Getting started with PowerShell<\/h2>\n\n\n\n<h2>Main<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_language_keywords?view=powershell-7.1\">Language Keywords and Grammar<\/a> including control flow<\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_operators?view=powershell-7.1\">about Operators<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/learn\/ps101\/06-flow-control?view=powershell-7.1#scripting\">Flow Control<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_script_blocks?view=powershell-7\">about_ScriptBlocks<\/a> (anonymous functions)<\/li><\/ul>\n\n\n\n<h2>Grammar<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_switch?view=powershell-7#syntax\">switch statements<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_functions?view=powershell-7#syntax\">functions<\/a><\/li><\/ul>\n\n\n\n<h2>Important Topics, Language quirks<\/h2>\n\n\n\n<h3><code class=\"\" data-line=\"\">For( ;; )<\/code> vs <code class=\"\" data-line=\"\">ForEach<\/code> vs <code class=\"\" data-line=\"\">ForEach-Object<\/code><\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_foreach?view=powershell-7.1\">about_ForEach<\/a> ( control loop)<\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_for?view=powershell-7.1\">about_For<\/a> ( control loop)<\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/foreach-object?view=powershell-7.1\">ForEach-Object<\/a> ( Note: this is a command, <strong>not a control loop )<\/strong><\/li><\/ul>\n\n\n\n<p>It&#8217;s first argument is a ScriptBlock so it appears like a control loop. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"584\" height=\"357\" src=\"https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2020\/11\/image-20.png\" alt=\"\" class=\"wp-image-871\" srcset=\"https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2020\/11\/image-20.png 584w, https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2020\/11\/image-20-300x183.png 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><figcaption>Notice <code class=\"\" data-line=\"\">return<\/code> verses <code class=\"\" data-line=\"\">break<\/code> in <code class=\"\" data-line=\"\">ForEach-Object<\/code> . Remember that the <code class=\"\" data-line=\"\">{ ... stuff ... }<\/code> in this case is an <code class=\"\" data-line=\"\">anonymous function<\/code> , not a language control statement<br>It is a <code class=\"\" data-line=\"\">parameter<\/code> to the <code class=\"\" data-line=\"\">ForEach-Object<\/code> . When not specified, it&#8217;s the <code class=\"\" data-line=\"\">-Process<\/code> Parameter <\/figcaption><\/figure>\n\n\n\n<h3>Flow Control with language Keywords<\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_break?view=powershell-7\">about_Break<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_return?view=powershell-7\">about_Return<\/a><\/li><li><a href=\"https:\/\/gist.github.com\/vexx32\/65a6dad29a16fcc698645949c808540d\">Reference Table for Loop Behavior in PowerShell<\/a> (from <a href=\"https:\/\/github.com\/vexx32\">vexx32<\/a> )<\/li><\/ul>\n\n\n\n<h2>Attributes for validation<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/cmdlet-attributes?view=powershell-7.1\">Attributes<\/a> TOC<ul><li>see: ValidatePattern, ValidateLength, ValidateSet<\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/parameter-attribute-declaration?view=powershell-7.1\">Parameter attribute<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/System.Management.Automation.Cmdlet.ShouldProcess?view=powershellsdk-7.0.0\">Cmdlet attribute<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2>Best Practices<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/strongly-encouraged-development-guidelines?view=powershell-7.1\">Strongly Encouraged Development Guidelines<\/a><\/li><li><a href=\"https:\/\/github.com\/PoshCode\/PowerShellPracticeAndStyle\">The PowerShell Best Practices and Style Guide<\/a>  &lt;github.com\/PoshCode\/PowerShellPracticeAndStyle&gt;<ul><li><a href=\"https:\/\/github.com\/PoshCode\/PowerShellPracticeAndStyle\/blob\/master\/Style-Guide\/Introduction.md\">Style Guide<\/a><\/li><\/ul><ul><li><a href=\"https:\/\/github.com\/PoshCode\/PowerShellPracticeAndStyle\/blob\/master\/Best-Practices\/Introduction.md\">PowerShell Best Practices<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>Naming<\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/approved-verbs-for-windows-powershell-commands?view=powershell-7.1\">Approved Verbs for PowerShell Commands<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/cmdlet-parameters?view=powershell-7.1\">Cmdlet Parameters<\/a> TOC<ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/standard-cmdlet-parameter-names-and-types?view=powershell-7.1\">Standard Cmdlet Parameter Names and Type<\/a><ul><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/property-parameters?view=powershell-7.1\">Property<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/quantity-parameters?view=powershell-7.1\">Quantity<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/developer\/cmdlet\/format-parameters?view=powershell-7.1\">Format<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting started with PowerShell Main Language Keywords and Grammar including control flow about Operators Flow Control about_ScriptBlocks (anonymous functions) Grammar switch statements functions Important Topics, Language quirks For( ;; ) vs ForEach vs ForEach-Object about_ForEach ( control loop) about_For ( control loop) ForEach-Object ( Note: this is a command, not a control loop ) It&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,14],"tags":[44,23,56,4,15],"_links":{"self":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/857"}],"collection":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/comments?post=857"}],"version-history":[{"count":9,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/857\/revisions"}],"predecessor-version":[{"id":1083,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/857\/revisions\/1083"}],"wp:attachment":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media?parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/categories?post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/tags?post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}