{"id":3959,"date":"2024-05-13T14:15:16","date_gmt":"2024-05-13T14:15:16","guid":{"rendered":"https:\/\/wiki.thomasandsofia.com\/?p=3959"},"modified":"2024-05-13T15:53:18","modified_gmt":"2024-05-13T15:53:18","slug":"powershell-variables-and-objects","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=3959","title":{"rendered":"Powershell: Variables and Arrays"},"content":{"rendered":"<h1>Powershell Variables<\/h1>\n<h2>Variables<\/h2>\n<p><strong>Variable Objects<\/strong><\/p>\n<p>All variables are Objects<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Variable declaration: $VarName<\/strong><\/p>\n<p><em>Note: These are NOT case sensitive! $MyVar is the same as $myvar<\/em><\/p>\n<p>Static variables (aka read-only constants):<\/p>\n<ul>\n<li>$true (True, 1)<\/li>\n<li>$false (False, 0)<\/li>\n<\/ul>\n<p><em>Note: You cannot assign variable to to True or False. Use their variable names.<\/em><\/p>\n<p><strong>Get Variable type<\/strong><\/p>\n<p><em>Note: Here we&#8217;re invoking a method of the variable object as shown later.<\/em><\/p>\n<pre>&gt; $MyVar = \"This is my variable.\"\r\n&gt; $MyVar.GetType()\r\nIsPublic IsSerial Name        BaseType\r\n-------- -------- ----        --------\r\nTrue     True     String      System.Object\r\n\r\n&gt;$MyVar.Length #Notice this is a property and not a method()\r\n20\r\n\r\n\r\n&gt; $MyVar = 5\r\n&gt; $MyVar.GetType()\r\nIsPublic IsSerial Name       BaseType\r\n-------- -------- ----       --------\r\nTrue     True     Int32      System.ValueType\r\n\r\n&gt; $MyVar = 5.3\r\n&gt; $MyVar.GetType()\r\nIsPublic IsSerial Name       BaseType\r\n-------- -------- ----       --------\r\nTrue     True     Double     System.ValueType<\/pre>\n<p><strong>To get all of the properties of an Object<\/strong><\/p>\n<pre>$MyVar | Select-Object -Property *<\/pre>\n<p><strong>To find the methods available:<\/strong><\/p>\n<pre>Get-Member -InputObject $MyVar<\/pre>\n<p><em>Note: This shows all the available methods and properties! Very Cool!<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>Arrays<\/h2>\n<p>Declare an array<\/p>\n<pre>&gt; $MyArray = @('str1','str2','str3')\r\n&gt; $MyArray\r\nstr1\r\nstr2\r\nstr3\r\n\r\n&gt; $MyArray.GetType()\r\nIsPublic IsSerial Name       BaseType \r\n-------- -------- ----       -------- \r\nTrue     True     Object[]   System.Array<\/pre>\n<p><strong>Array Indexes<\/strong><\/p>\n<p>Indexes start at 0<\/p>\n<pre>&gt; $MyArray[1]\r\nstr2<\/pre>\n<p><strong>Append a new element to an array<\/strong><\/p>\n<pre>$MyArray += 'str4'<\/pre>\n<p><strong>Array Key=&gt;Value pairs (These are called &#8216;Hash Tables&#8217; or &#8216;Mappings&#8217;)<br \/>\n<\/strong><\/p>\n<ul>\n<li>Use brackets &#8216;{..}&#8217; instead of &#8216;( )&#8217;.<\/li>\n<li>Use a semicolon &#8216;;&#8217; instead of commas &#8216;,&#8217; to separate values.<\/li>\n<\/ul>\n<pre>&gt; $MyArray = @{key1 = 'value1'; 'key2' = 'value2'}\r\n&gt; $MyArray\r\nName     Value\r\n----     -----\r\nkey1     value1\r\nkey2     value2<\/pre>\n<p><em>Note: Does not appear that quotes are required around the keys when defining, however, I&#8217;d recommend using them as are required with other languages.<\/em><\/p>\n<p><strong>View Values<\/strong><\/p>\n<pre>&gt; $myArray.'key1'\r\nvalue1\r\n&gt; $myArray['key2']\r\nvalue2<\/pre>\n<p><strong>Append a new index<\/strong><\/p>\n<pre>&gt; $myArray.Add('NewKey','NewValue')<\/pre>\n<p><strong>Edit a value<\/strong><\/p>\n<pre>&gt; $myArray.Set_Item('NewKey','DifferentValue')\r\n&gt; $myArray['NewKey']='DifferentValue'<\/pre>\n<p><em>Note you must use quotes around the keys.<\/em><\/p>\n<p><strong>Removing an array index (element)<\/strong><\/p>\n<pre>&gt; $myArray.Remove(\"key1\")\r\n&gt; $myArray\r\nName     Value\r\n----     -----\r\nkey2     value2\r\nNewValue DifferentValue<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Powershell Variables Variables Variable Objects All variables are Objects &nbsp; Variable declaration: $VarName Note: These are NOT case sensitive! $MyVar is the same as $myvar Static variables (aka read-only constants): $true (True, 1) $false (False, 0) Note: You cannot assign variable to to True or False. Use their variable names. Get Variable type Note: Here ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=3959\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82,83],"tags":[],"class_list":["post-3959","post","type-post","status-publish","format-standard","hentry","category-powershell","category-programming"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3959"}],"version-history":[{"count":3,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3959\/revisions"}],"predecessor-version":[{"id":3962,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3959\/revisions\/3962"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}