{"id":10307,"date":"2022-02-15T05:04:00","date_gmt":"2022-02-15T05:04:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=10307"},"modified":"2024-04-17T11:32:37","modified_gmt":"2024-04-17T11:32:37","slug":"basics-of-powershell","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/basics-of-powershell\/","title":{"rendered":"Basics of PowerShell"},"content":{"rendered":"\n<p>PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS. In this article we will discuss about what is PowerShell, its features, cmdlet commands, data types and operator with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-container-based-architecture\"><strong>What is PowerShell?<\/strong><\/h2>\n\n\n\n<p>Microsoft designed PowerShell is an object-oriented scripting language and is designed mainly for the system administrators. It helps to control &amp; automate the administration of the Window OS and other applications. It combines the flexibility of scripting, command-line speed, and the power of a GUI-based admin tool.<\/p>\n\n\n\n<p>PowerShell has a well-integrated command-line utilities\/ command for the operation system. It is a simple way to get information about server and is more secure than running VBScript or other scripting languages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Features of PowerShell<\/strong><\/h2>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>PowerShell Remoting: The remoting feature&nbsp;allows the execution of PowerShell cmdlets on remote systems that help to manage a set of remote computers from one single machine.<\/li>\n\n\n\n<li>Background Jobs: PowerShell&nbsp;introduced the&nbsp;concept of background jobs, which run cmdlets and scripts asynchronously on local and remote machines in the background without affecting the interface or interacting with the console.<\/li>\n\n\n\n<li>Scheduled Job: A scheduled job&nbsp;is like a&nbsp;background job; both jobs are running asynchronously in the background without interrupting the user interface, but the difference is that a background job must be started manually<\/li>\n\n\n\n<li>Script debugging: As in&nbsp;Visual Studio, you can&nbsp;set breakpoints on lines, columns, functions, variables, and commands.&nbsp;<\/li>\n\n\n\n<li>Error handling: PowerShell provides error-handling mechanism through the Try{ }, Catch{ }, and Finally { } statements as in .NET languages<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-cloud-and-version-control-agnostic\"><strong>PowerShell Cmdlets<\/strong><\/h4>\n\n\n\n<p>A cmdlet is a PowerShell command with a predefined function<\/p>\n\n\n\n<p><strong>Format<\/strong><\/p>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li><strong>Get&nbsp;<\/strong>\u2014 To get something<\/li>\n\n\n\n<li><strong>Start<\/strong>&nbsp;\u2014 To run something<\/li>\n\n\n\n<li><strong>Out<\/strong>&nbsp;\u2014 To output something<\/li>\n\n\n\n<li><strong>Stop<\/strong>&nbsp;\u2014 To stop something that is running<\/li>\n\n\n\n<li><strong>Set&nbsp;<\/strong>\u2014 To define something<\/li>\n\n\n\n<li><strong>New<\/strong>&nbsp;\u2014 To create something<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-standardized-pipeline-creations-and-definitions-resources-as-objects\"><strong>PowerShell Data Types<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Data Type Name<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>[Array]<\/td><td>Array<\/td><\/tr><tr><td>[Bool]<\/td><td>Value is TRUE or FALSE<\/td><\/tr><tr><td>[DateTime]<\/td><td>Date and time<\/td><\/tr><tr><td>[Guid]<\/td><td>Globally unique 32-byte identifier<\/td><\/tr><tr><td>[HashTable]<\/td><td>Hash table, collection of key-value pairs<\/td><\/tr><tr><td>[Int32], [Int]<\/td><td>32-bit integers<\/td><\/tr><tr><td>[PsObject]<\/td><td>PowerShell object<\/td><\/tr><tr><td>[Regex]<\/td><td>Regular&nbsp; expression<\/td><\/tr><tr><td>[ScriptBlock]<\/td><td>PowerShell script block<\/td><\/tr><tr><td>[Single], [Float]<\/td><td>Floating point number<\/td><\/tr><tr><td>[String]<\/td><td>String<\/td><\/tr><tr><td>[Switch]<\/td><td>PowerShell switch parameter<\/td><\/tr><tr><td>[TimeSpan]<\/td><td>Time interval<\/td><\/tr><tr><td>[XmlDocument]<\/td><td>XML document<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-graphical-pipeline-view\"><strong>Variables in PowerShell<\/strong><\/h4>\n\n\n\n<p><strong>Simple variable<\/strong><br>All variables in PowerShell begin with a US dollar sign ($). The simplest example of this is:<br>$f = &#8220;bar\u201c<br>Arrays<br>Array declaration in PowerShell is almost the same as instantiating any other variable,. The items in the array are declared by separating them by commas(,):<br>$myArrayOfInts = 1,2,3,4<br>$myArrayOfStrings = &#8220;1&#8221;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4\u201c<\/p>\n\n\n\n<p><strong>Adding to an Array<\/strong><br>Adding to an array is as simple as using the + operator:<br>$myArrayOfInts = $myArrayOfInts + 5 # now contains 1,2,3,4 &amp; 5!<\/p>\n\n\n\n<p><strong>Combining arrays together<\/strong><br>Again this is as simple as using the + operator<br>$myArrayOfInts = 1,2,3,4<br>$myOtherArrayOfInts = 5,6,7<br>$myArrayOfInts = $myArrayOfInts + $myOtherArrayOfInts<\/p>\n\n\n\n<p><strong>Remove-Item Variable:f<\/strong><\/p>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>To remove a variable from memory, one can use the Remove-Item cmdlet.<\/li>\n\n\n\n<li>Another method to remove variable is to use Remove-Variable cmdlet and its alias rv<\/li>\n<\/ul>\n\n\n\n<p>$var = \u2018Some Variable\u2019 #Define variable &#8216;var&#8217; containing the string &#8216;Some Variable\u2019<br>$var<br><strong>Remove-Variable -Name var<\/strong><br>$var<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-parallel-steps\"><strong><strong>Operators<\/strong><\/strong><\/h3>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>An operator is a character that represents an action.<\/li>\n\n\n\n<li>It tells the compiler\/interpreter to perform speci\ufb01c mathematical, relational or logical operation and produce \ufb01nal result.<\/li>\n\n\n\n<li>PowerShell interprets in a speci\ufb01c way and categorizes accordingly like arithmetic operators strings and other data types.<\/li>\n\n\n\n<li>Along with the basic operators, PowerShell has several operators that save time and coding e\ufb00ort (e.g.: -like, -match, -replace, etc.).<\/li>\n\n\n\n<li>Simple comparison operators:<\/li>\n<\/ul>\n\n\n\n<p>          2 -eq 2 # Equal to (==)<br>          2 -ne 4 # Not equal to (!=)<br>          5 -gt 2 # Greater-than (&gt;)<br>          5 -ge 5 # Greater-than or equal to (&gt;=)<br>          5 -lt 10 # Less-than (&lt;) 5<\/p>\n\n\n\n<p><strong>String comparison operators:<br><\/strong>&#8220;MyString&#8221; -like &#8220;<em>String&#8221; # Match using the wildcard character (<\/em>)<br>&#8220;MyString&#8221; -notlike &#8220;Other<em>&#8221; # Does not match using the wildcard character (<\/em>)<br>&#8220;MyString&#8221; -match &#8216;^String$&#8217; # Matches a string using regular expressions<br>&#8220;MyString&#8221; -notmatch &#8216;^Other$&#8217; # Does not match a string using regular expressions<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-standardized-plugin-mechanism-docker-based\"><strong><strong>Redirection Operator<\/strong><\/strong><\/h3>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>PowerShell redirection operators are using specific characters to specify the output to files Please check the list below:<\/li>\n<\/ul>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>\u2013 All output<br>1 \u2013 Success output<br>2 \u2013 Errors<br>3 \u2013 Warnings messages<br>4 \u2013 Verbose Output<br>5 \u2013 Debug messages<br>6 \u2013 Informational message<\/li>\n<\/ul>\n\n\n\n<p>In order to use * , 3 , 4 , 5 you need to have PowerShell 3.0 or above.<br>Those four types were introduced in Powershell 3.0.<br>The newer type that has been introduced in PowerShell 5.0 is 6. You need to have PowerShell 5.0 or above to used it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-everything-configurable-in-the-ui-ux-and-in-code\"><strong><strong>Assignment Operator<\/strong><\/strong><\/h3>\n\n\n\n<p><strong>Simple arithmetic:<\/strong><br>$var = 1 # Assignment. Sets the value of a variable to the specified value<br>$var += 2 # Addition. Increases the value of a variable by the specified value<br>$var -= 1 # Subtraction. Decreases the value of a variable by the specified value<br>$var *= 2 # Multiplication. Multiplies the value of a variable by the specified value<br>$var \/= 2 # Division. Divides the value of a variable by the specified value<br>$var %= 2 # Modulus. Divides the value of a variable by the specified value and then<br># assigns the remainder (modulus) to the variable<\/p>\n\n\n\n<p><strong>Increment and decrement:<\/strong><br>$var++ # Increases the value of a variable, assignable property, or array element by<br>1 $var&#8211; # Decreases the value of a variable, assignable property, or array element by 1<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-live-pipeline-debug-with-breakpoints\"><strong><strong>Mixing Operand Types<\/strong><\/strong><\/h3>\n\n\n\n<p>Note: The type of the left operand dictates the behavior<\/p>\n\n\n\n<p>For Addition<br>&#8220;4&#8221; + 2 # Gives &#8220;42&#8221;<br>4 + &#8220;2&#8221; # Gives 6<br>1,2,3 + &#8220;Hello&#8221; # Gives 1,2,3,&#8221;Hello&#8221;<br>&#8220;Hello&#8221; + 1,2,3 # Gives &#8220;Hello1 2 3&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-native-support-for-kubernetes-helm-and-docker\"><strong><strong>Redirection Operator<\/strong><\/strong><\/h3>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>&gt; Sends output to the specified file<\/li>\n\n\n\n<li>&gt;&gt; \u2013 Appends output to the content of the specified file<\/li>\n\n\n\n<li>2&gt; \u2013 Sends errors to the specified file.<\/li>\n\n\n\n<li>2&gt;&gt; \u2013 Appends errors to the content of the specified file<\/li>\n\n\n\n<li>2&gt;&amp;1 \u2013 Sends errors and success output to the success output stream<\/li>\n\n\n\n<li>3&gt; \u2013 Sends warnings to the specified file<\/li>\n\n\n\n<li>3&gt;&gt; \u2013 Appends warnings to the contents of the specified file.<\/li>\n\n\n\n<li>3&gt;&amp;1 \u2013 Sends warnings and success output to the success output stream<\/li>\n\n\n\n<li>4&gt; \u2013 Sends verbose output to the specified file<\/li>\n\n\n\n<li>4&gt;&gt; \u2013 Appends verbose output to the contents of the specified file<\/li>\n\n\n\n<li>4&gt;&amp;1 \u2013 Sends verbose output and success output to the success output stream<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-saas-on-prem-and-hydrid-installation-methods\"><strong><strong>Why should we learn PowerShell?<\/strong><\/strong><\/h3>\n\n\n\n<ul class=\"blog-detail-list wp-block-list\">\n<li>All the&nbsp;<strong>server products<\/strong>&nbsp;that Microsoft is producing now can be&nbsp;<strong>managed<\/strong>&nbsp;through PowerShell.<\/li>\n\n\n\n<li>Performing the tasks like updating an active directory manually may take hours to complete. By using PowerShell you can complete it using a single command in less time.<\/li>\n\n\n\n<li>Many of the&nbsp;<strong>GUI interfaces<\/strong>&nbsp;that Microsoft has been designing for its various products are front-end&nbsp;interfaces to PowerShell.<\/li>\n\n\n\n<li>Windows PowerShell&nbsp;<strong>Can Reveal &#8220;Hidden&#8221; Information<\/strong>&nbsp;Not Available in the Admin Center.<\/li>\n\n\n\n<li>Windows PowerShell is Great at Filtering Data. It lets us do&nbsp;<strong>cross-product<\/strong>&nbsp;management.<\/li>\n\n\n\n<li>PowerShell provides Windows management instruction, enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management<\/li>\n\n\n\n<li>Having a solid knowledge of PowerShell scripting will enable us to reduce the time on many administrative functions, without having to purchase and implement third-party tools.<\/li>\n<\/ul>\n\n\n\n<p>Stay tuned for more updates in PowerShell \u2013 Part II, where we will discuss examples on, we can execute it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS. In this article we will discuss about what is PowerShell, its features, cmdlet commands, data types and operator with examples. What is PowerShell? Microsoft designed PowerShell is&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/basics-of-powershell\/\">Continue reading <span class=\"screen-reader-text\">Basics of PowerShell<\/span><\/a><\/p>\n","protected":false},"author":26,"featured_media":16364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[59,61],"tags":[],"class_list":["post-10307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-app-modernization-blogs","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp","author_info":{"display_name":"Vijay","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/vijayg\/"},"views":4819,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2022\/02\/New_Blogs4_Powershell.webp",800,440,false]},"uagb_author_info":{"display_name":"Vijay","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/vijayg\/"},"uagb_comment_info":0,"uagb_excerpt":"PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS. In this article we will discuss about what is PowerShell, its features, cmdlet commands, data types and operator with examples. What is PowerShell? Microsoft designed PowerShell is&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=10307"}],"version-history":[{"count":1,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10307\/revisions"}],"predecessor-version":[{"id":16365,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/10307\/revisions\/16365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16364"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=10307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=10307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=10307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}