{"id":15,"date":"2014-11-06T03:43:25","date_gmt":"2014-11-06T03:43:25","guid":{"rendered":"http:\/\/parablog-wordpress.dockerbox.rei.moe\/?p=15"},"modified":"2014-11-06T04:20:40","modified_gmt":"2014-11-06T04:20:40","slug":"arrays-in-powershell","status":"publish","type":"post","link":"https:\/\/blog.lewys.eu\/?p=15","title":{"rendered":"Arrays in powershell"},"content":{"rendered":"<p><em>Reference sheet for my powershell class<\/em><\/p>\n<p>Arrays seem to be like small temporary databases ? \ud83d\ude00<\/p>\n<p>&nbsp;<\/p>\n<p>Creating an Array:<\/p>\n<p>C:\\PS&gt;<strong>$numbers = @(4,7,9,11,20)<\/strong><span class=\"Apple-converted-space\">\u00a0\u00a0\u00a0\u00a0 \/\/creates array and populates entries with data<\/span><br \/>\nC:\\PS&gt;<strong>write-host $numbers<\/strong><span class=\"Apple-converted-space\">\u00a0\u00a0\u00a0\u00a0 \/\/prints entire array<br \/>\n<\/span><\/p>\n<p>Number 9 can be retrieved with<\/p>\n<p>C:\\PS&gt;<strong>$numbers[2]<\/strong>\u00a0\u00a0\u00a0 \/\/prints 3rd item in array (integers start at 0<\/p>\n<p>similarly<\/p>\n<p>C:\\PS&gt;<strong>write-host $numbers[2] &#8220;this is the second item in the array&#8221;<\/strong>\u00a0\u00a0\u00a0 \/\/includes text at the end and uses the write-host command<\/p>\n<p>C:\\PS&gt;<strong>$names = @(&#8220;Martin&#8221;, &#8220;Emma&#8221;, &#8220;Alan&#8221;, &#8220;Laura&#8221;)<\/strong><span class=\"Apple-converted-space\"><br \/>\n<\/span>\u00a0\u00a0\u00a0 \/\/this is how words are stored into an array.<\/p>\n<p>Or you can <span class=\"Apple-converted-space\">\u00a0<\/span>can create an empty array and then add elements later:<\/p>\n<p>C:\\PS&gt;<strong>$MoreNames = @()<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nC:\\PS&gt;<strong>$MoreNames += &#8220;Emma&#8221;<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nC:\\PS&gt;<strong>$MoreNames += &#8220;John&#8221;<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nC:\\PS&gt;<strong>$MoreNames += &#8220;Gerald&#8221;<\/strong><span class=\"Apple-converted-space\"><br \/>\n<\/span><\/p>\n<p>The size of the array can be retrieved with the commands<\/p>\n<p>C:\\PS&gt;<strong>$ArrayName.count<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>OR<br \/>\nC:\\PS&gt;<strong>$ArrayName.length<\/strong><\/p>\n<p>(Both these seem to return the same values)<\/p>\n<p>An array can be auto populated with a number range by using double dots to tell powershell to fill in the gap. eg:<\/p>\n<p>C:\\PS&gt;<strong id=\"yui_3_13_0_3_1415236514211_254\">$range = @(10..50)<\/strong><span class=\"Apple-converted-space\"><br \/>\n<\/span><\/p>\n<p>Will create an array containing the numbers 10, through to and including 50.<\/p>\n<p>To retrieve the last item from the array, you can use either<\/p>\n<p>C:\\PS&gt;<strong>$array[$array.Length-1]<\/strong><span class=\"Apple-converted-space\"><br \/>\n<\/span><\/p>\n<p>OR<\/p>\n<p>C:\\PS&gt;<strong id=\"yui_3_13_0_3_1415236514211_255\">$array[-1]<\/strong><\/p>\n<p>To retrieve the FIRST item in an array, you first get the length, you substitute the<strong> [-1]<\/strong> with <strong>[-$array.count] <\/strong>. Or any variation of these to your desired item.<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0T<\/span>o store the output from a command to an array, use cmdlets:<\/p>\n<p>C:\\PS&gt;<strong>$array = Get-Process<\/strong><\/p>\n<p>To create an array that already has item spaces for a set amount of items, you can use:<\/p>\n<p>C:\\PS&gt;<strong>$NameArray = New-Object String[] 8<span class=\"Apple-converted-space\">\u00a0<\/span><\/strong><br \/>\nC:\\PS&gt;<strong>$IntArray = New-Object Int[] 12<span class=\"Apple-converted-space\"><br \/>\n<\/span><\/strong><\/p>\n<p>Beginning Assignment:<\/p>\n<p>CLS\u00a0 #clears all the crap off the screen from the last thing i ran \ud83d\ude00<\/p>\n<p>write-host &#8220;Press any key to Start&#8221;<br \/>\n$x = $host.UI.RawUI.ReadKey(&#8220;NoEcho,IncludeKeyDown&#8221;) #waits for a key to be pushed before continuing<br \/>\nwrite-host &#8220;&#8221;<br \/>\nwrite-host &#8220;Generating random numbers&#8221;<\/p>\n<p>$Item0 = Get-Random -minimum 0 -maximum 99 #Generates a random number between 0 and 99 and stores it in $Item0<br \/>\n$Item1 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item2 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item3 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item4 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item5 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item6 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item7 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item8 = Get-Random -minimum 0 -maximum 99<br \/>\n$Item9 = Get-Random -minimum 0 -maximum 99<\/p>\n<p>$RandomArray = @($Item0, $Item1, $Item2, $Item3,$Item4,$Item5,$Item6,$Item7,$Item8,$Item9) #stores the randomly generated numbers into $RandomArray<br \/>\nwrite-host &#8220;&#8221;<br \/>\nwrite-host &#8220;Now all the numbers are generated and stored in the `$RandomArray` array,&#8221;<br \/>\nwrite-host &#8220;we will use a sort that puts the largest into the&#8221;<br \/>\nwrite-host &#8220;variable until its reached the end of the array.&#8221;<br \/>\nwrite-host &#8220;&#8221;<br \/>\n[array]::sort($RandomArray) #sorts the array ?<\/p>\n<p>write-host &#8220;&#8221;<br \/>\nwrite-host &#8220;The largest number in the array is&#8221; $RandomArray[-1] #writes out the last item of $RandomArray<br \/>\nwrite-host &#8220;&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>update: next exercise involved those freaking factorials, i am once again lost and unsure how to continue &lt;,&lt;<\/p>\n<p>WHY DO I GET STUCK ON THESE<\/p>\n<p>also, i cant get loops into my head in this stupid language, why is C\/C++ easier than this ?<\/p>\n<p>isn&#8217;t powershell meant to be a baby language or something?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reference sheet for my powershell class Arrays seem to be like small temporary databases ? \ud83d\ude00 &nbsp; Creating an Array: C:\\PS&gt;$numbers = @(4,7,9,11,20)\u00a0\u00a0\u00a0\u00a0 \/\/creates array and populates entries with data C:\\PS&gt;write-host $numbers\u00a0\u00a0\u00a0\u00a0 \/\/prints entire array Number 9 can be retrieved with C:\\PS&gt;$numbers[2]\u00a0\u00a0\u00a0 \/\/prints 3rd item in array (integers start at 0 similarly C:\\PS&gt;write-host $numbers[2] &#8220;this &hellip; <a href=\"https:\/\/blog.lewys.eu\/?p=15\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Arrays in powershell<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,22],"tags":[6,5,3,2,4],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-educational","category-reference","tag-lesson","tag-notes","tag-powershell","tag-programming","tag-rant"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":4,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}