{"id":1808,"date":"2014-05-13T00:00:00","date_gmt":"2014-05-13T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/powershell-one-liners-collections-hashtables-arrays-and-strings\/"},"modified":"2019-01-23T16:02:11","modified_gmt":"2019-01-23T16:02:11","slug":"powershell-one-liners-collections-hashtables-arrays-and-strings","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/powershell-one-liners-collections-hashtables-arrays-and-strings\/","title":{"rendered":"PowerShell One-Liners:  Collections, Hashtables, Arrays and Strings"},"content":{"rendered":"<div id=\"pretty\">\n<p>This series is in four parts: This is part 3<\/p>\n<ul>\n<li><a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners-help,-syntax,-display-and--files\/\"><b>Part 1<\/b>: Help, Syntax, Display and Files\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners-variables,-parameters,-properties,-and-objects\/\"><b>Part 2<\/b>: Variables, Parameters, Properties, and Objects,<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners--collections,-hashtables,-arrays-and-strings\/\"> <b>Part 3<\/b>:\u00a0Collections, Hashtables, Arrays and Strings<\/a>\u00a0<\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners--collections,-hashtables,-arrays-and-strings\/\"> <b>Part 4<\/b>:\u00a0<\/a><a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners--accessing,-handling-and-writing-data-\/\">Accessing, Handling and Writing Data\u00a0<\/a><\/li>\n<\/ul>\n<div class=\"pullout\">\n<div>\n<h3>Notes on Using the Tables<\/h3>\n<\/div>\n<p>A command will typically use full names of cmdlets but the examples will often use aliases for brevity. Example: Get-Help has aliases man and help. This has the side benefit of showing you both long and short names to invoke many commands.<\/p>\n<p>Most tables contain either 3 or 4 columns: a description of an action; the generic command syntax to perform that action; an example invocation of that command; and optionally an output column showing the result of that example where feasible.<\/p>\n<p>For clarity, embedded newlines (`n) and embedded return\/newline combinations (`r`n) are highlighted as shown.<\/p>\n<p>Many actions in PowerShell can be performed in more than one way. The goal here is to show just the simplest which may mean displaying more than one command if they are about equally straightforward. In such cases the different commands are numbered with square brackets (e.g. &#8220;[1]&#8221;). Multiple commands generally mean multiple examples, which are similarly numbered.<\/p>\n<p>Most commands will work with PowerShell version 2 and above, though some require at least version 3. So if you are still running v2 and encounter an issue that is likely your culprit.<\/p>\n<p>The vast majority of commands are built-in, i.e. supplied by Microsoft. There are a few sprinkled about that require loading an additional module or script, but their usefulness makes them worth including in this compendium. These &#8220;add-ins&#8221; will be demarcated with angle brackets, e.g. &lt;&lt;pscx&gt;&gt; denotes the popular PowerShell Community Extensions (<a href=\"http:\/\/pscx.codeplex.com\/\">http:\/\/pscx.codeplex.com\/<\/a>).<\/p>\n<\/div>\n<p>This is a multi-part series of PowerShell reference charts. Here you will details of the two fundamental data structures of PowerShell: the collection (array) and the hash table (dictionary), examining everything from creating, accessing, iterating, ordering, and selecting. Part 3 also covers converting between strings and arrays, and rounds out with techniques for searching, most commonly applicable to files (searching both directory structures as well as file contents)<\/p>\n<p>Be sure to review <a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners-help,-syntax,-display-and--files\/\">parts 1<\/a> <a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/powershell-one-liners-variables,-parameters,-properties,-and-objects\/\">and 2<\/a>, though, which begin by showing you how to have PowerShell itself help you figure out what you need to do to accomplish a task, covering the help system as well as its handy command-line intellisense. They also cover locations, files, and paths (the basic currency of a shell); key syntactic constructs; ways to cast your output in list, table, grid, or chart form; and key PowerShell concepts of variables, parameters, properties, and objects.<\/p>\n<p>Part 4 is your information source for a variety of input and output techniques: reading and writing files; writing the various output streams; file housekeeping operations; and various techniques related to CSV, JSON, database, network, and XML.<\/p>\n<p>Each part of this series is available as an online reference here at Simple-Talk.com, a wide version <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1989-PSOL3Big.html\">here<\/a> as well as a downloadable wallchart in PDF format for those who prefer a printed copy near at hand. Please keep in mind though that this is a quick reference, not a tutorial. So while there are a few brief introductory remarks for each section, there is very little explanation for any given incantation. But do not let that scare you off-jump in and try things! You should find more than a few &#8220;aha!&#8221; moments ahead of you!<\/p>\n<h2>Contents<\/h2>\n<ul>\n<li><a href=\"#first\">Collections (Arrays)<\/a><\/li>\n<li><a href=\"#second\">Collection Selection<\/a><\/li>\n<li><a href=\"#third\">Collection Union, Intersection, Uniqueness.<\/a><\/li>\n<li><a href=\"#fourth\">Collection Ordering.<\/a><\/li>\n<li><a href=\"#fifth\">Collections and LINQ.<\/a><\/li>\n<li><a href=\"#sixth\">Hash Tables (Dictionaries)<\/a><\/li>\n<li><a href=\"#seventh\">Hash Table Access and Iteration.<\/a><\/li>\n<li><a href=\"#eighth\">Strings to Arrays: Splitting.<\/a><\/li>\n<li><a href=\"#ninth\">Arrays to Strings: Joining.<\/a><\/li>\n<li><a href=\"#tenth\">String Search.<\/a><\/li>\n<li><a href=\"#eleventh\">File Search.<\/a><\/li>\n<\/ul>\n<h2 id=\"first\">Collections (Arrays)<\/h2>\n<p>Collections are everywhere in PowerShell; they are the most prevalent of all its data structures. Cmdlets and pipes let you pass around objects, but keep in mind that they usually pass around objects (plural), not just an object (singular). So it is important to have a good sense about what you can do with collections. Most of the collections you will encounter, therefore, are generated by some cmdlet. But occasionally you need to create your own, so the first few entries here show you how to do that. This section also presents crucial entries for iterating through collections and comparing collections.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize literal array with at least 2 elements<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] @(value, value, value, &#8230;)<\/p>\n<p>[2] value, value, value, &#8230;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $myArray = @( &#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;,&#8221;d&#8221;,&#8221;e&#8221;,&#8221;f&#8221;,&#8221;g&#8221;,&#8221;h&#8221; )<\/p>\n<p>[2] $myArray = &#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;,&#8221;d&#8221;,&#8221;e&#8221;,&#8221;f&#8221;,&#8221;g&#8221;,&#8221;h&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize literal array with one element<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] @( value )<\/p>\n<p>[2] , value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $myArray = @(25)<\/p>\n<p>[2] $myArray = ,25<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize a strongly-typed array<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[typeName[]] $name = values<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[int[]] $a = 1,2,3,4<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Iterate array\/collection by pipeline<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array | ForEach-Object { &#8230; $_ &#8230; }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1,2,3 | % { &#8220;item $_&#8221; }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>item 1<\/p>\n<p>item 2<\/p>\n<p>item 3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Iterate array\/collection by non-pipeline<\/p>\n<\/td>\n<td valign=\"top\">\n<p>foreach ($var in $array) { commands }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>foreach ($item in &#8220;a&#8221;,&#8221;b&#8221;) { $item }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>a<\/p>\n<p>b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Iterate collection with initialization\/finalization<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array | % { beginBlock } { commands } { endBlock }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Return just odd-numbered elements:<\/p>\n<p>&#8216;v1&#8242;,&#8217;v2&#8242;,&#8217;v3&#8217;,&#8217;v4&#8217;| foreach {$i=1} { if ($i++ % 2) {$_} } {&#8220;done&#8221;}<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>v1<\/p>\n<p>v3<\/p>\n<p>done<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Ensure value is an array<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] @(any)<\/p>\n<p>[2] ,any<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $a = @(Get-Service | select -first 1) ; $a.length<\/p>\n<p>[2] $a = ,(Get-Service | select -first 1) ; $a.length<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1<\/p>\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Fill array with the same value efficiently (see <a href=\"http:\/\/stackoverflow.com\/a\/17877292\/115690\">How to fill an array efficiently in PowerShell<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td valign=\"top\">\n<p>In order from most to least efficient:<\/p>\n<p>[1] $a = ,2 * $length<\/p>\n<p>[2] [int[]]$a = [System.Linq.Enumerable]::Repeat(2, $length)<\/p>\n<p>[3] $a = foreach ($i in 1..$length) { 2 }<\/p>\n<p>[4] [int[]]$a = -split &#8220;2 &#8221; * $length<\/p>\n<p>[5] $a = for ($i = 0; $i -lt $length; $i++) { 2 }<\/p>\n<p>[6] $a = 1..$length | %{ 2 }<\/p>\n<p>[7] $a = @(); for ($i = 0; $i -lt $length; $i++) { $a += 2 }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Compare arrays (independent of order) returning differences<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Compare-Object object1 object2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>compare (1..5) (4..1)<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>InputObject SideIndicator<\/p>\n<p>&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5 &lt;=<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Compare arrays where order is significant<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Compare-Object object1 object2 -Sync 0<\/p>\n<\/td>\n<td valign=\"top\">\n<p>diff (1..3) (3..1) -Sync 0<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>InputObject SideIndicator<\/p>\n<p>&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 =&gt;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 &lt;=<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 =&gt;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 &lt;=<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Compare arrays returning single Boolean indicating a match or not<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@(compare object1 object2).length -eq 0<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] @(compare (1..5) (5..1)).length -eq 0<\/p>\n<p>[2] @(compare (1..5) (4..1)).length -eq 0<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>True<\/p>\n<p>False<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"second\">Collection Selection<\/h2>\n<p>After iteration, selecting is probably the most common thing to do with a collection. Entries here show how to select one or more elements, contiguous or not, as well as equivalents to the common take and skip operations common to many collection structures. (Note that the output column has been condensed by removing line breaks; the true output will actually show each element on a separate line, except as indicated.)<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select single element by index<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array[index]<\/p>\n<\/td>\n<td valign=\"top\">\n<p>(1,2,3,4,5)[0]<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select multiple specific elements<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -Index m,n<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1..10 | select -index 0,4,9<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1 5 10<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select contiguous elements via array notation<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array[m..n]<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>\u00a0(1..10)[1..4]<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>2 3 4 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select contiguous elements<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -Index (m..n)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1..10 | select -index (1..4)<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>2 3 4 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select first n elements (head)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -First <i>n<\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$n = 2; 1,2,3,4,5 | select -first $n<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1 2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select last n elements (tail)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -Last <i>n<\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$n = 4; 1,2,3,4,5 | select -last $n<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>2 3 4 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select n elements after skipping m elements<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -First n -Skip m<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1..10 | select -skip 3 -first 4<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>4 5 6 7<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select all elements except the first n<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-Object -Skip <i>n<\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$n = 2; 1,2,3,4,5 | select -skip $n<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>3 4 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select all elements except the last n<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] any | Select-Object -Skip n -Last LargeInt<\/p>\n<p>[2] $txt = any; $txt[0..($txt.length-n-1)]<i><\/i><\/p>\n<p>[3] any | Skip-Object -Last n &lt;&lt;pscx&gt;&gt;<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $n = 2; 1..5 | Select-Object -skip $n -last 10000000<\/p>\n<p>[2] $n = 2; $txt = 1..5; $txt[0..($txt.length-$n-1)]<\/p>\n<p>[3] $n = 2; 1..5 | Skip-Object -last $n<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1 2 3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Display all elements on one line<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;array-expression&#8221;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = 3,5,7; &#8220;$a&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>3 5 7 #really on one line!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"third\">Collection Union, Intersection, Uniqueness<\/h2>\n<p>Entries in this section let you do more complex operations on collections. Note that simple concatenation propagates duplicates whereas union and intersection are strict set operations: they do not include duplicate values. Entries here also show how to obtain just the unique elements in a collection as well as adding to collections.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Concatenate two collections<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array1 + $array2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@(&#8220;apple&#8221;,&#8221;pear&#8221;) + @(&#8220;apple&#8221;,&#8221;orange&#8221;)<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>apple<\/p>\n<p>pear<\/p>\n<p>apple<\/p>\n<p>orange<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Set union<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array1 + $array2 | select -Unique<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@(&#8220;apple&#8221;,&#8221;pear&#8221;) + @(&#8220;apple&#8221;,&#8221;orange&#8221;) | select -uniq<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>apple<\/p>\n<p>pear<\/p>\n<p>orange<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Set intersection<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array1 | select -Unique |<br \/>\n where { $array2 -contains $_ }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@(1,2,5,9) | select -uniq | ? { @(2,4,9,16) -contains $_ }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>2<\/p>\n<p>9<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Set difference (<a href=\"http:\/\/stackoverflow.com\/q\/9815192\/115690\">In PowerShell how can I check if all items from one array exist in a second array?<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$array1 | select -Unique |<br \/>\n where { $array2 -notcontains $_ }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1,2,3,4,2,3 | select -uniq |? { 1,3,4,5 -notcontains $_ }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get unique elements, case-sensitive, sorted<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any-sorted | Get-Unique<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;abc&#8221;, &#8220;abc&#8221; , &#8220;Abc&#8221;, &#8220;def&#8221; | Get-Unique<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>Abc<\/p>\n<p>def<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get unique elements, case-sensitive, unsorted<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] any | Sort-Object -CaseSensitive| Get-Unique<\/p>\n<p>[2] any | Select-Object -Unique<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] &#8220;abc&#8221;, &#8220;Abc&#8221;, &#8220;def&#8221;, &#8220;abc&#8221; | sort -case | Get-Unique<\/p>\n<p>[2] &#8220;abc&#8221;, &#8220;Abc&#8221;, &#8220;def&#8221;, &#8220;abc&#8221; | select -unique<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>Abc<\/p>\n<p>def<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get unique elements, case-insensitive<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Sort-Object -Unique<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;abc&#8221;, &#8220;Abc&#8221;, &#8220;def&#8221;, &#8220;abc&#8221; | sort -unique<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>def<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Add an element to an array<\/p>\n<\/td>\n<td valign=\"top\">\n<p>array += element<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = 1,2,3; $a += 4; $a<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>1 2 3 4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Add an element to multiple arrays (see <a href=\"http:\/\/stackoverflow.com\/a\/21433360\/115690\">How to append elements to multiple arrays on the same line?<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>arrays = arrays |% {,($_ += element)}<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = @(&#8220;a&#8221;,&#8221;b&#8221;); $b = @(1,2); <br \/>\n $a,$b = $a,$b |% {,($_ += &#8216;foo&#8217;)}; &#8220;$a &#8212; $b&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>a b foo &#8212; 1 2 foo<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"fourth\">Collection Ordering<\/h2>\n<p>Once you have a collection chances are you might want to re-order it per the needs of your application. You can do this with derived properties almost as easily as with simple named properties. The last few entries show how to apply sorting to file contents as well.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort collection of strings<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Sort-Object<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;ab12&#8221;, &#8220;ab1&#8221;, &#8220;ab103&#8221; | sort<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>ab1<\/p>\n<p>ab103<\/p>\n<p>ab12<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort collection of strings by derived property<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Sort-Object -property propertyExpression<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;ab12&#8221;, &#8220;ab1&#8221;, &#8220;ab103&#8221; | sort { [int]($_ -replace &#8216;\\D&#8217;) }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>ab1<\/p>\n<p>ab12<\/p>\n<p>ab103<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort collection of objects by named property<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Sort-Object -property propertyName<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls \\windows\\system32\\dwm*.dll | sort -property length | select name, length | ft -auto<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0Length<\/p>\n<p>&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0&#8212;&#8212;<\/p>\n<p>dwmapi.dll\u00a0 \u00a0115200<\/p>\n<p>dwmredir.dll 172544<\/p>\n<p>dwmcore.dll 2219520<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort file of specified data type<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Content filespec |Sort-Object\u00a0 { [type]$_ }<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>gc numbers.txt | sort { [int]$_ }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort whitespace-delimited text file by first field<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Content filespec |Sort-Object { [type](-split $_)[0] }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>gc lines.txt | sort { [double](-split $_)[0] }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort whitespace-delimited text file by last field<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Content filespec |Sort-Object { [type](-split $_)[-1] }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>gc lines.txt | sort { [int](-split $_)[-1] }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"fifth\">Collections and LINQ<\/h2>\n<p>If you are used to relying on LINQ-to-Object operators in C# so much that you may are almost compelled to reject PowerShell out of hand, fear not! PowerShell provides an assortment of basic LINQ-equivalent operations out-of-the-box, as detailed in the entries below. Many of them you have already seen if you have read the above sections on collections. Note that the one key thing you do not get with these standard PowerShell operators, though, is lazy evaluation. If you are keen on that, I refer you to Bart DeSmet&#8217;s LINQ Through PowerShell (<a href=\"http:\/\/bit.ly\/1j9Y7cS\">http:\/\/bit.ly\/1j9Y7cS<\/a>).<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>LINQ Method<\/p>\n<\/td>\n<td valign=\"top\">\n<p>PowerShell Cmdlet<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Projection<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select-Object<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Process | Select-Object -Property Name, WorkingSet, StartTime<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Restriction<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Where<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Where-Object<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-ChildItem | Where-Object {\u00a0 $PSItem.Length -gt 1000 }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Ordering<\/p>\n<\/td>\n<td valign=\"top\">\n<p>OrderBy<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Sort-Object<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-ChildItem | Sort-Object -Property length -Descending<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Grouping<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GroupBy<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Group-Object<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Service | Group-Object Status<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Set Operation<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Distinct<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] Get-Unique<\/p>\n<p>[2] Sort-Object -Unique<\/p>\n<p>[3] Select-Object -Unique<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] &#8220;abc&#8221;, &#8220;def&#8221;, &#8220;abc&#8221; | Sort-Object | Get-Unique<\/p>\n<p>[2] &#8220;abc&#8221;, &#8220;def&#8221;, &#8220;abc&#8221; | Sort-Object -unique<\/p>\n<p>[3] Get-ChildItem *.cs -r | Select-String &#8220;public.*void&#8221; | Select-Object -uniq Path<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Partitioning<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Take<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select-Object -First<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Process | Select-Object -First 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Partitioning<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Skip<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select-Object -Skip<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Process | Select-Object -Skip 5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Quantifiers<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Any<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] See <a href=\"http:\/\/stackoverflow.com\/a\/1499486\/115690\"> PowerShell equivalent of LINQ Any()?<\/a><\/p>\n<p>(JaredPar&#8217;s solution)<\/p>\n<p>[2] See <a href=\"http:\/\/stackoverflow.com\/a\/22090065\/115690\"> PowerShell equivalent of LINQ Any()?<\/a><\/p>\n<p>(Paolo Tedesco&#8217;s solution)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] function Test-Any() { begin { $any = $false } process { $any = $true } end { $any } }<\/p>\n<p>1..4 |Where { $_ -gt 5 } | Test-Any<\/p>\n<p>[2] function Test-Any {<\/p>\n<p>\u00a0\u00a0\u00a0 [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest)<\/p>\n<p>\u00a0\u00a0\u00a0 begin { $any = $false }<\/p>\n<p>\u00a0\u00a0\u00a0 process { if (-not $any -and (&amp; $EvaluateCondition $ObjectToTest)) { $any = $true } }<\/p>\n<p>\u00a0\u00a0\u00a0 end { $any } }<\/p>\n<p>1..4 | Test-Any { $_ -gt 5 }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Quantifiers<\/p>\n<\/td>\n<td valign=\"top\">\n<p>All<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td valign=\"top\">\n<p>function Test-All {<\/p>\n<p>\u00a0\u00a0\u00a0 [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest)<\/p>\n<p>\u00a0\u00a0\u00a0 begin { $all = $true }<\/p>\n<p>\u00a0\u00a0\u00a0 process { if (!(&amp; $EvaluateCondition $ObjectToTest)) { $all = $false } }<\/p>\n<p>\u00a0\u00a0\u00a0 end { $all } }<\/p>\n<p>1..4 | Test-Any { $_ -gt 0 }<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"sixth\">Hash Tables (Dictionaries)<\/h2>\n<p>Hash tables are the other ubiquitous data structure that you will encounter as well as generate yourself. As they are more involved than a simple collection, there are more varied ways to create one. This section provides a synopsis of common techniques for generating hash tables. The next section shows you how to access its members. Hash table values are not strongly typed, as you can see in the first entry, which mixes strings and integers. You can use a standard .NET dictionary, though, for strong typing.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize literal hash table<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@{ label = value; &#8230; }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@{<\/p>\n<p>&#8220;i1&#8243;=&#8221;bird&#8221;<\/p>\n<p>&#8220;i2&#8243;=256<\/p>\n<p>&#8220;i3&#8243;=&#8221;cat&#8221; }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name Value<\/p>\n<p>&#8212;- &#8212;&#8211;<\/p>\n<p>i2\u00a0\u00a0 256<\/p>\n<p>i3\u00a0\u00a0 cat<\/p>\n<p>i1\u00a0\u00a0 bird<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize literal hash table (minimal punctuation)<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td valign=\"top\">\n<p>@&#8221;<\/p>\n<p>i1=bird<\/p>\n<p>i2=256<\/p>\n<p>i3=cat<\/p>\n<p>&#8220;@ | ConvertFrom-StringData<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name Value<\/p>\n<p>&#8212;- &#8212;&#8211;<\/p>\n<p>i3\u00a0\u00a0 cat<\/p>\n<p>i2\u00a0\u00a0 256<\/p>\n<p>i1\u00a0\u00a0 bird<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash table from CSV with header row<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Import-Csv $file |<br \/>\n foreach { $hash = @{} } { $hash[$_.key] = $_.value}<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Assumes headers &#8220;first,second&#8221;<\/p>\n<p>Import-Csv $file | % { $hash = @{} } { $hash[$_.first] = $_.second}<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash table from CSV without header row (see <a href=\"https:\/\/social.technet.microsoft.com\/Forums\/windowsserver\/en-US\/6bf96040-99c7-4d4f-88fe-1499d416706c\/convert-a-2-columns-csv-into-a-hash-table?forum=winserverpowershell\"> Convert a 2 columns CSV into a hash table<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>(any -replace &#8216;,&#8217;, &#8216;=&#8217;) -join &#8220;`n&#8221; | ConvertFrom-StringData<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash = ((Get-Content text.csv) -replace &#8216;,&#8217;, &#8216;=&#8217;) -join &#8220;`n&#8221; |<br \/>\n ConvertFrom-StringData<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash table from a file where a simple separator is insufficient; specify a regex with two subgroups picking out the key and the value.<\/p>\n<p>(<a href=\"http:\/\/stackoverflow.com\/a\/8798093\/115690\">How to construct hash table from file using powershell?<\/a>)<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash = @{};<\/p>\n<p>Get-Content $file |<\/p>\n<p>foreach { if ($_ -match $regex)<\/p>\n<p>\u00a0\u00a0 { $hash[$matches[1]] = $matches[2] }<\/p>\n<p>\u00a0}<\/p>\n<\/td>\n<td valign=\"top\">\n<p>The example matches input lines like &#8220;&lt;i1&gt;=&lt;bird&gt;&#8221;<br \/>\n selecting &#8220;i1&#8221; as the key and &#8220;bird&#8221; as the value.<\/p>\n<p>$hash = @{}<\/p>\n<p>Get-Content $file |<\/p>\n<p>% { if ($_ -match &#8216;^&lt;(.*)&gt;=&lt;(.*)&gt;&#8217;)<\/p>\n<p>\u00a0\u00a0 { $hash[$matches[1]]=$matches[2] }<\/p>\n<p>\u00a0}<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize data structure from PS code literal<\/p>\n<p>(little need to ever do this; it is just to illustrate what the following entries do from a file)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Out-String | Invoke-Expression<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;@{ X = &#8216;x&#8217;; Y = &#8216;y&#8217; }&#8221; | Out-String | iex<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name\u00a0\u00a0\u00a0 Value<\/p>\n<p>&#8212;-\u00a0\u00a0\u00a0 &#8212;&#8211;<\/p>\n<p>Y\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0y<\/p>\n<p>X\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash table from PS code file<\/p>\n<\/td>\n<td valign=\"top\">\n<p>variable = filespec.ps1<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Assume\u00a0 file contains e.g. @{ X = &#8216;x&#8217;; Y = &#8216;y&#8217; }<\/p>\n<p>$a = .\\data.ps1\u00a0<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>same as above<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash table from text file<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Get-Content filespec | Out-String | Invoke-Expression<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Assume\u00a0 file contains e.g. @{ X = &#8216;x&#8217;; Y = &#8216;y&#8217; }<\/p>\n<p>$a = gc .\\data.txt | Out-String | iex<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>same as above<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize hash of hash tables from INI file<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash = Get-IniFile file<\/p>\n<p>&lt;&lt;code from <a href=\"http:\/\/bit.ly\/1e5MEvj\">Get-IniFile<\/a>&gt;&gt;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;[Install]`nA=640`nB=0x403f`n[Extras]`nOpt=10`nValue=0&#8221; | <br \/>\n Set-Content test.ini; $ini = Get-IniFile .\\test.ini<\/p>\n<p>[1] $ini[&#8220;Install&#8221;][&#8220;A&#8221;]<\/p>\n<p>[2] $ini.Install.B<\/p>\n<p>[3] $ini.Extras<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>[1]\u00a0 640<\/p>\n<p>[2]\u00a0 0x403f<\/p>\n<p>[3]<\/p>\n<p>Name\u00a0 Value<\/p>\n<p>&#8212;-\u00a0 &#8212;&#8211;<\/p>\n<p>Value 0\u00a0\u00a0\u00a0<\/p>\n<p>Opt\u00a0\u00a0 10 <i><\/i><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Initialize a strongly-typed hash<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$dict = New-Object &#8216;System.\u00c2\u00adCollections.\u00c2\u00adGeneric.Dictionary\u00c2\u00ad[type,type]&#8217;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$dict = New-Object &#8216;System.\u00c2\u00adCollections.\u00c2\u00adGeneric.\u00c2\u00adDictionary\u00c2\u00ad[string,int]&#8217;<\/p>\n<p>$dict.Fred = 25<\/p>\n<p>$dict.Mary = &#8220;abc&#8221; # runtime error<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"seventh\">Hash Table Access and Iteration<\/h2>\n<p>Once you have a hash, there are two things you might want to do with it: do something with a single element or do something with every element. As the first line item shows, there are three different syntaxes possible to access a single element. (Most entries in this section refer to the same simple hash setup in the previous section.)<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Access hash element by key value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $hash[$key]<\/p>\n<p>[2] $hash.key<\/p>\n<p>[3] $hash.Item($key)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $myHash[&#8220;i2&#8221;]<\/p>\n<p>[2] $myHash.i3<\/p>\n<p>[3] $myHash.Item(&#8220;i1&#8221;)<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>256<\/p>\n<p>cat<\/p>\n<p>bird<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Iterate through hash with enumerator<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash.GetEnumerator() |<br \/>\n foreach { &#8230; $_.Key &#8230; $_.Value &#8230;}<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$myHash.GetEnumerator() |<\/p>\n<p>\u00a0\u00a0\u00a0 % { &#8220;key={0}, value={1}&#8221; -f $_.key, $_.value }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>key=i2, value=256<\/p>\n<p>key=i3, value=cat<\/p>\n<p>key=i1, value=bird<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Iterate through hash with keys<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash.Keys | foreach { &#8230; $_ &#8230; $hash[$_] &#8230; }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $myHash.Keys | % {&#8220;k={0}, v={1}&#8221; -f $_,$myHash.Item($_) }<\/p>\n<p>[2] $myHash.Keys | % {&#8220;k={0}, v={1}&#8221; -f $_,$myHash[$_] }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>k=i2, v=256<\/p>\n<p>k=i3, v=cat<\/p>\n<p>k=i1, v=bird<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Reverse a hash<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$hash.Keys |<br \/>\n foreach {$Rhash=@{}} { $Rhash[$hash[$_]] = $_ }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$h = @{ &#8220;i1&#8243;=&#8221;bird&#8221;; &#8220;i2&#8243;=256; &#8220;i3&#8243;=&#8221;cat&#8221; }; $h.Keys |<br \/>\n % { $Rhash=@{} } { $Rhash[$h[$_]] = $_ } { $Rhash }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name Value<\/p>\n<p>&#8212;- &#8212;&#8211;<\/p>\n<p>bird i1<\/p>\n<p>cat\u00a0 i3<\/p>\n<p>256\u00a0 i2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Modify entries with a given value<\/p>\n<\/td>\n<td valign=\"top\">\n<p>@($table.GetEnumerator()) |<br \/>\n where {$_.Value -eq oldValue} |<br \/>\n foreach { $table[$_.Key] = newValue }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$table = @{ &#8220;A1&#8243;=3; &#8220;A2&#8243;=3; &#8220;A3&#8243;=6; &#8220;A4&#8243;=12; };<br \/>\n @($table.GetEnumerator()) |<br \/>\n ? {$_.Value -eq 3} |<br \/>\n % { $table[$_.Key]=4 }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Name Value<\/p>\n<p>&#8212;- &#8212;&#8211;<\/p>\n<p>A1\u00a0\u00a0 4<\/p>\n<p>A2\u00a0\u00a0 4<\/p>\n<p>A4\u00a0\u00a0 12<\/p>\n<p>A3\u00a0\u00a0 6<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"eighth\">Strings to Arrays: Splitting<\/h2>\n<p>This section provides an assortment of techniques going in one direction, i.e. splitting up strings into arrays. Here you can see examples of how to split on whitespace, line breaks, simple delimiters, and regular expressions.\u00a0 The next section illustrates how to go back the other direction.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on whitespace<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-split string<\/p>\n<\/td>\n<td valign=\"top\">\n<p># Note that `t = tab and `n = newline:<\/p>\n<p>-split &#8220;one \u00a0\u00a0\u00a0two`tthree`nfour&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>one<\/p>\n<p>two<\/p>\n<p>three<\/p>\n<p>four<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on simple delimiter<br \/>\n (escape any regex metachars with backslash)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>string -split delimiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] &#8220;one,two,three&#8221; -split &#8220;,&#8221;<\/p>\n<p>[2] &#8220;one#-#two#-#three&#8221; -split &#8220;#-#&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>one<\/p>\n<p>two<\/p>\n<p>three<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on regular expression<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] [regex]::split(string, regex)<\/p>\n<p>[2] string -split regex<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] [regex]::split(&#8220;123#456#apple&#8221;, &#8220;#(?!\\d)&#8221;)<\/p>\n<p>[2] &#8220;123#456#apple&#8221; -split &#8220;#(?!\\d)&#8221;<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>123#456<\/p>\n<p>apple<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on regular expression with options<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[regex]::split(string, regex, options)<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] [regex]::split(&#8220;Apple_aPPle_APple&#8221;, &#8220;ppl&#8221;, &#8220;IgnoreCase&#8221;)<\/p>\n<p>[2] [regex]::split(&#8220;Apple_aPPle_APple&#8221;, &#8220;ppl&#8221;,<\/p>\n<p>[System.Text.RegularExpressions.\u00c2\u00adRegexOptions]::\u00c2\u00adIgnoreCase)<i><\/i><\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>A<\/p>\n<p>e_a<\/p>\n<p>e_A<\/p>\n<p>e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on complex single-char expression<br \/>\n (see about_split: <a href=\"http:\/\/bit.ly\/1dIizfS\">about_Split<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>string -split scriptBlock<\/p>\n<\/td>\n<td valign=\"top\">\n<p># $_ matches any single character:<\/p>\n<p>&#8220;Brobdingnag&#8221; -split {$_ -eq &#8220;n&#8221; -or $_ -eq &#8220;o&#8221;}<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Br<\/p>\n<p>bdi<\/p>\n<p>g<\/p>\n<p>ag<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split string on complex single-char expression using external criterion<\/p>\n<\/td>\n<td valign=\"top\">\n<p>string -split scriptBlock<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$i = 5; &#8220;a,b#c!d&#8221; <br \/>\n \u00a0\u00a0\u00a0 -split { if ($i -gt 3) {$_ -eq &#8220;,&#8221;} else {$_ -eq &#8220;#&#8221;} }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>a<\/p>\n<p>b#c!d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split pipeline data on Windows line breaks<\/p>\n<p>(Out-String uses Environment.NewLine)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>string -split &#8220;`r`n&#8221;<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>(Get-Content test.txt | Out-String) -split &#8220;`r`n&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split by line, retaining whitespace<br \/>\n (here strings use just newline character)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>hereString -split &#8220;`n&#8221;<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$data = @&#8221;<\/p>\n<p>one<\/p>\n<p>two<\/p>\n<p>three`t`t`t<\/p>\n<p>&#8220;@<\/p>\n<p>$b = $data -split &#8220;`n&#8221;; &#8220;&lt;$($b[2])&gt;&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>&lt;three\u00a0\u00a0 &gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split by line, trimming whitespace<\/p>\n<\/td>\n<td valign=\"top\">\n<p>hereString -split &#8220;`n&#8221; | % { $_.Trim() }<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$b = ($data -split &#8220;`n&#8221;).Trim(); &#8220;&lt;$($b[2])&gt;&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>&lt;three&gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split by line, retaining empty entries<\/p>\n<\/td>\n<td valign=\"top\">\n<p>hereString -split &#8220;`n&#8221;<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;one`n`ntwo`nthree&#8221; -split&#8221;`n&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>one<\/p>\n<p>&nbsp;<\/p>\n<p>two<\/p>\n<p>three<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>11<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Split by line, skipping empty entries<\/p>\n<\/td>\n<td valign=\"top\">\n<p>hereString.Split(&#8220;`n&#8221;, [System.\u00c2\u00adStringSplitOptions]::\u00c2\u00adRemoveEmptyEntries)<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;one`n`ntwo`nthree&#8221;.Split(&#8220;`n&#8221;, [System.StringSplitOptions]::\u00c2\u00adRemoveEmptyEntries)<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>one<\/p>\n<p>two<\/p>\n<p>three<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"ninth\">Arrays to Strings: Joining<\/h2>\n<p>Going back the other way-joining array elements together into a string-is simpler than splitting, of course, so this section offers fewer variations than last section, which illustrated how to split up a string.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Join strings with no delimiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>-join array<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] -join (&#8220;abc&#8221;,&#8221;def&#8221;)<\/p>\n<p>[2] $a = &#8220;abc&#8221;,&#8221;def&#8221;; -join $a<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abcdef<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Join string array with default delimiter ($OFS)<\/p>\n<p><a href=\"http:\/\/bit.ly\/1ai7EJ5\">about_Preference_Variables<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;array&#8221;<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = &#8220;abc&#8221;,&#8221;def&#8221;;\u00a0 &#8220;$a&#8221;<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc def<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Join string array using Windows line breaks<\/p>\n<\/td>\n<td valign=\"top\">\n<p>array | Out-String<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;abc&#8221;,&#8221;def&#8221;,&#8221;ghi&#8221;| Out-String<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc`r`ndef`r`nghi<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Join strings with specified delimiter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $OFS = delimiter; &#8220;array&#8221;<\/p>\n<p>[2] array -join delimiter<\/p>\n<p>[3] [string]::join(delimiter, array)<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] $OFS = &#8220;##&#8221;; &#8220;$(&#8216;abc&#8217;, &#8216;def&#8217;, &#8216;ghi&#8217;)&#8221;<\/p>\n<p>[2] &#8220;abc&#8221;,&#8221;def&#8221;,&#8221;ghi&#8221; -join &#8220;##&#8221;<\/p>\n<p>[3] [string]::join(&#8220;##&#8221;, &#8220;abc&#8221;,&#8221;def&#8221;,&#8221;ghi&#8221;)<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc##def##ghi<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"tenth\">String Search<\/h2>\n<p>How can any developer survive without grep? Just take a look at Select-String to find out. It has essentially all the bells and whistles that grep has. You can display context before and after a match (-Context). You can print with or without filenames, line numbers, and other properties (by piping into Select-Object and selecting appropriate properties). You can just print matched files, too, without the matched text; you will find that illustrated in the File Search section next. Here are just a variety of starter recipes to get you thinking about how to fine-tune your searches. Also take a look at Select-StringAligned (available from <a href=\"http:\/\/bit.ly\/1nlzgrU\"> http:\/\/bit.ly\/1nlzgrU<\/a>) that lets you align your matches when you are displaying file names with them instead of having the matches after the ragged right edge of the file names; this reveals patterns in some searches in a startling fashion.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>Output<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Replace string in selected files recursively (see <a href=\"http:\/\/stackoverflow.com\/a\/11369356\/115690\">PowerShell: Recursively Replace String in Select Sub-files of a Directory<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>foreach ($f in gci -r -include pattern)<\/p>\n<p>\u00a0\u00a0\u00a0 { (gc $f.fullname) |<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 % { $_ -replace regex, replacement }\u00a0 |<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sc $f.fullname<\/p>\n<p>\u00a0\u00a0\u00a0 }<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select first occurrence of a pattern on each line<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-String &#8220;(pattern)&#8221; | foreach { $_.Matches[0].Groups[1].Value }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = &#8220;abc def&#8221;,&#8221;foobar&#8221;,&#8221;12345-,-678&#8243;;<br \/>\n $a | sls &#8220;([a-z]+)&#8221; | % { $_.Matches[0].Groups[1].Value }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>foobar<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line the text after a given pattern (<a href=\"http:\/\/stackoverflow.com\/a\/13527211\/115690\">PowerShell &#8211; split string &amp; output all text to the right<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-String &#8216;(?&lt;=pattern)(.*)&#8217; | select -expa matches | select -expa value |\u00a0 foreach { $_.trim() }<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a = &#8220;abc-,-def&#8221;,&#8221;12345-,-678&#8243;; $a | sls &#8216;(?&lt;=-,-)(.*)&#8217; | select -expa matches | select -expa value |\u00a0 % { $_.trim() }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>def<\/p>\n<p>678<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line the text before a given pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-String &#8216;(.*)(?=pattern)&#8217; | select -expa matches | select -expa value | foreach { $_.trim() }<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a | Select-String &#8216;(.*)(?=-,-)&#8217; | select -expa matches | select -expa value |\u00a0 % { $_.trim() }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>12345<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line text by position (column)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | % { $_.substring(int,int) }<i><\/i><\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a | % { $_.substring(2,3) }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>c-,<\/p>\n<p>345<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line one column from CSV<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] Import-Csv file| select -ExpandProperty name<\/p>\n<p>[2] any | ConvertFrom-Csv -Header nameList |<br \/>\n select -ExpandProperty name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a | ConvertFrom-Csv -Header &#8220;V1&#8221;, &#8220;V2&#8221; | select -expa V1<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc-<\/p>\n<p>12345-<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line multiple columns from CSV<\/p>\n<\/td>\n<td valign=\"top\">\n<p>[1] Import-Csv file | foreach { formatString -f $_.name1, $_.name2, &#8230; }<\/p>\n<p>[2] any | ConvertFrom-Csv -Header names | foreach { formatString -f $_.name1, $_.name2, &#8230; }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>$a | ConvertFrom-Csv -Header &#8220;V1&#8221;, &#8220;V2&#8221; | % { &#8220;{0} \/ {1}&#8221; -f $_.V1, $_.V2 }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc- \/ -def<\/p>\n<p>12345- \/ -678<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Select from each line one column from delimited file, no headers<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Import-Csv file -Header field-list -Delimiter delimiter | select &#8211; ExpandProperty field<\/p>\n<\/td>\n<td valign=\"top\">\n<p>import-csv -Header name,id,amt text.csv -Delimiter . |<br \/>\n select -expa name<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Convert multi-line text input into records (<a href=\"http:\/\/stackoverflow.com\/a\/13588065\/115690\">Formatting text in PowerShell<\/a>)<\/p>\n<\/td>\n<td valign=\"top\">\u00a0<\/td>\n<td valign=\"top\">\n<p>gc .\\test.txt -ReadCount 2 | % {$_ -join &#8216;,&#8217; } |<br \/>\n ConvertFrom-Csv \u00a0-Header Col1,Col2<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Filter out blank lines<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Where { $_ }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&#8220;abc&#8221;,&#8221;&#8221;,&#8221;def&#8221; | ? { $_ }<\/p>\n<\/td>\n<td class=\"mono\" valign=\"top\">\n<p>abc<\/p>\n<p>def<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"eleventh\">File Search<\/h2>\n<p>The previous section, String Search, focused on finding text within files (as well as within collections in general). This section, in contrast, focuses on finding files: files that contain text and files whose names contain text. Get-ChildItem is at the heart of every entry in this section (though I use its ls alias for brevity). Then, depending on the recipe, you typically apply either Select-String or Where-Object to achieve the desired results.<\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b>#<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p>Action<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Command<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Example<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List file names and lines in multiple files containing a pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls filespec | Select-String pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls . -Recurse *.cs | Select-String &#8220;public.*void&#8221;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List just lines in multiple files containing pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>(ls filespec | Select-String pattern).Line<\/p>\n<\/td>\n<td valign=\"top\">\n<p>(ls . -r *.cs | sls &#8220;public.*void&#8221;).Line<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files containing a pattern, returning strings<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls filespec | Select-String pattern | select -Unique Path<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls *.cs -r | sls &#8220;public.*void&#8221; | select -uniq Path<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files containing a pattern, returning FileInfo objects<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls filespec | Where { Select-String string $_ -Quiet }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls *.cs -r | ? { sls -quiet &#8220;public.*void&#8221; $_ }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files not containing a pattern, returning strings<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls filespec |<br \/>\n Where { !(Select-String string $_ -Quiet) }.FullName<\/p>\n<\/td>\n<td valign=\"top\">\n<p>(ls -r *.xml| ? { !(sls -quiet &#8220;home &#8221; $_ ) }).FullName<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files not containing a pattern, returning FileInfo objects<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls filespec | Where { !(Select-String string $_ -Quiet) }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r *.xml| ? { !(sls -quiet &#8220;home&#8221;\u00a0 $_ ) }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>7<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files with names matching a wildcard pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r expression<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r *.html<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files with names matching a regex pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls . options | Where { $_.Name -match pattern }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r *.xml | ? { $_.name -match &#8220;abc{0,3}.*\\.xml&#8221; }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>List files with path \u00a0matching a regex pattern<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls . options | Where { $_ -match pattern }<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r *.xml | ? { $_ -match &#8220;this\\\\sub\\\\path&#8221; }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Count occurrences of a string per file<\/p>\n<\/td>\n<td valign=\"top\">\n<p>any | Select-String pattern | Group path<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ls -r *.xml | sls home | group path | select count,name | ft -auto<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"twelveth\">Conclusion<\/h2>\n<p>That&#8217;s it for part 3; keep an eye out for more in the near future! While I have been over the recipes presented numerous times to weed out errors and inaccuracies, I think I may have missed one. If you locate it, please share your findings in the comments below!<\/p>\n<p class=\"note\"><strong>Note:<\/strong> Out of necessity, the version of the tables in the articles is somewhat compressed. If you find them hard to read, then there is a <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1989-PSOL3Big.html\">wide version of the article available here<\/a>, and a PDF version is available from the link below.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The way to learn PowerShell is to browse and nibble, rather than to sit down to a formal five-course meal. In his continuing series on Powershell one-liners, Michael Sorens provides Fast Food for busy professionals who want results quickly and aren&#8217;t too faddy. Part 3 has, as its tasty confections,  collections, hashtables, arrays and strings.&hellip;<\/p>\n","protected":false},"author":221868,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[35],"tags":[95506,4635,4871],"coauthors":[6802],"class_list":["post-1808","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-automate","tag-powershell","tag-sysadmin"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221868"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1808"}],"version-history":[{"count":7,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1808\/revisions"}],"predecessor-version":[{"id":75130,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1808\/revisions\/75130"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1808"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}