{"id":2431,"date":"2013-12-13T00:01:00","date_gmt":"2013-12-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/13\/using-the-powershell-contains-operator\/"},"modified":"2013-12-13T00:01:00","modified_gmt":"2013-12-13T00:01:00","slug":"using-the-powershell-contains-operator","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-the-powershell-contains-operator\/","title":{"rendered":"Using the PowerShell Contains Operator"},"content":{"rendered":"<p><strong>Summary<\/strong>: &nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell <strong>Contains<\/strong> operator to work with arrays.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-Step\/dp\/0735663394\/ref=tmm_pap_title_0?ie=UTF8&amp;qid=1383770622&amp;sr=8-1\" target=\"_blank\">Windows PowerShell 3.0 Step by Step<\/a>, published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7331.Step%20by%20Step%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7331.Step%20by%20Step%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<h2>Examine contents of an array<\/h2>\n<p>To verify input that is received from the command line, you can use the <strong style=\"font-size: 12px\">Contains<\/strong><span style=\"font-size: 12px\"> operator to examine the contents of an array of possible values. This following technique illustrates an array of three values that is created and stored in the variable <\/span><strong style=\"font-size: 12px\">$noun<\/strong><span style=\"font-size: 12px\">. The <\/span><strong style=\"font-size: 12px\">Contains<\/strong><span style=\"font-size: 12px\"> operator is then used to see if the array contains &#8220;hairy-nosed wombat&#8221;. Because the <\/span><strong style=\"font-size: 12px\">$noun<\/strong><span style=\"font-size: 12px\"> variable does not have an array element that is equal to the string &#8220;hairy-nosed wombat,&#8221; the <\/span><strong style=\"font-size: 12px\">Contains<\/strong><span style=\"font-size: 12px\"> operator returns False.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $noun = &#8220;cat&#8221;,&#8221;dog&#8221;,&#8221;rabbit&#8221;<br \/> PS C:\\&gt; $noun -contains &#8220;hairy-nosed wombat&#8221;<br \/> False<br \/> PS C:\\&gt;<\/p>\n<p>If an array contains a match, the <strong>Contains<\/strong> operator returns True, as shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $noun = &#8220;cat&#8221;,&#8221;dog&#8221;,&#8221;rabbit&#8221;<br \/> PS C:\\&gt; $noun -contains &#8220;rabbit&#8221;<br \/> True<br \/> PS C:\\&gt;<\/p>\n<p>The <strong>Contains<\/strong> operator returns True only when there is an exact match. Partial matches return False. This is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $noun = &#8220;cat&#8221;,&#8221;dog&#8221;,&#8221;rabbit&#8221;<br \/> PS C:\\&gt; $noun -contains &#8220;bit&#8221;<br \/> False<br \/> PS C:\\&gt;<\/p>\n<p>The <strong>Contains<\/strong> operator is case insensitive. Therefore, it will return True when matched, regardless of case:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $noun = &#8220;cat&#8221;,&#8221;dog&#8221;,&#8221;rabbit&#8221;<br \/> PS C:\\&gt; $noun -contains &#8220;Rabbit&#8221;<br \/> True<br \/> PS C:\\&gt;<\/p>\n<p>If you need to perform a case sensitive match, you can use the case sensitive version of the <strong>Contains<\/strong> operator, <strong>-ccontains<\/strong>. As shown here, it will return True only if the case of the string matches the value that is contained in the array:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $noun = &#8220;cat&#8221;,&#8221;dog&#8221;,&#8221;rabbit&#8221;<br \/> PS C:\\&gt; $noun -ccontains &#8220;Rabbit&#8221;<br \/> False<br \/> PS C:\\&gt; $noun -ccontains &#8220;rabbit&#8221;<br \/> True<br \/> PS C:\\&gt;<\/p>\n<p>In the Get-AllowedComputers.ps1 script, a single command-line parameter is created, which is used to hold the name of the target computer for the WMI query. The computer parameter is a string, and it receives the default value from the environmental drive. This is a good technique because it ensures that the script will have the name of the local computer, which could then be used to produce a report of the results. If you set the value of the computer parameter to <strong>LocalHost<\/strong>, you never know what computer the results belong to. This is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Param([string]$computer = $env:computername)<\/p>\n<p>The <strong>Get-AllowedComputer<\/strong> function is used to create an array of permitted computer names and to check the value of the <strong>$computer<\/strong> variable to see if it is present. If the value of the <strong>$computer<\/strong> variable is present in the array, the <strong>Get-AllowedComputer<\/strong> function returns True. If the value is missing from the array, the <strong>Get-AllowedComputer<\/strong> function returns False.<\/p>\n<p>The array of computer names is created by using the <strong>Get-Content<\/strong> cmdlet to read a text file that contains a list of computer names. The text file, servers.txt, is a plain ASCII text file that has a list of computer names on individual lines, as shown in the following image:<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.hsg-12-13-13-1.png\"><img decoding=\"async\" title=\"Image of note\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.hsg-12-13-13-1.png\" alt=\"Image of note\" \/><\/a><\/p>\n<p>A text file of computer names is easier to maintain than a hard-coded array that is embedded into the script. In addition, the text file can be placed on in central shared folder, and it can be used by many different scripts. The <strong>Get-AllowedComputer<\/strong> function is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Function Get-AllowedComputer([string]$computer)<br \/> {<br \/> &nbsp;$servers = Get-Content -path c:\\fso\\servers.txt <br \/> &nbsp;$servers -contains $computer<br \/> } #end Get-AllowedComputer function<\/p>\n<p>Because the <strong>Get-AllowedComputer<\/strong> function returns a Boolean value (True\/False) it can be used directly in an IF statement to determine if the value that is supplied for the <strong>$computer<\/strong> variable is on the permitted list. If the <strong>Get-AllowedComputer<\/strong> function returns True, the <strong>Get-WmiObject<\/strong> cmdlet is used to query for BIOS information from the target computer. This is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">if(Get-AllowedComputer -computer $computer)<br \/> &nbsp;{<br \/> &nbsp;&nbsp; Get-WmiObject -class Win32_Bios -Computer $computer<br \/> &nbsp;}<\/p>\n<p>On the other hand, if the value of the <strong>$computer<\/strong> variable is not found in the <strong>$servers<\/strong> array, a string that states the computer is not an allowed computer is displayed:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Else<br \/> &nbsp;{<br \/> &nbsp; &#8220;$computer is not an allowed computer&#8221;<br \/> &nbsp;}<\/p>\n<p>Following is the complete Get-AllowedComputer.ps1 script:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\"><strong>Get-AllowedComputer.ps1<br \/> <\/strong>Param([string]$computer = $env:computername)<\/p>\n<p> Function Get-AllowedComputer([string]$computer)<br \/> {<br \/> &nbsp;$servers = Get-Content -path c:\\fso\\servers.txt <br \/> &nbsp;$servers -contains $computer<br \/> } #end Get-AllowedComputer function<\/p>\n<p> # *** Entry point to Script ***<\/p>\n<p> if(Get-AllowedComputer -computer $computer)<br \/> &nbsp;{<br \/> &nbsp;&nbsp; Get-WmiObject -class Win32_Bios -Computer $computer<br \/> &nbsp;}<br \/> Else<br \/> &nbsp;{<br \/> &nbsp; &#8220;$computer is not an allowed computer&#8221;<br \/> &nbsp;}<\/p>\n<h2>Test for properties<\/h2>\n<p>You are not limited to only testing for specified computer names in the <strong>Get-AllowedComputer<\/strong> function. All you need to do is add additional information to the text file, as shown in the following image:<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7206.hsg-12-13-13-2.png\"><img decoding=\"async\" title=\"Image of note\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7206.hsg-12-13-13-2.png\" alt=\"Image of note\" \/><\/a><\/p>\n<p>A couple of modifications to the Get-AllowedComputer.ps1 script are all that is required to turn it into the Get-AllowedComputerAndProperty.ps1 script. The first is to add an additional command line parameter to allow the user to choose which property to display:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Param([string]$computer = $env:computername,[string]$property=&#8221;name&#8221;)<\/p>\n<p>Next, the signature to the <strong>Get-AllowedComputer<\/strong> function is changed to permit passing the <strong>Property<\/strong> name. Instead of directly returning the results of the <strong>Contains<\/strong> operator, the returned values are stored in variables. The <strong>Get-AllowedComputer<\/strong> function first checks to see if the <strong>$servers<\/strong> array contains the computer name. It then checks to see if the <strong>$servers<\/strong> array contains the <strong>Property<\/strong> name. Each of the resulting values is stored in variables. The two variables are then added together, and the result returns to the calling code. When two Boolean values are added together, only the &ldquo;True and True&rdquo; case is equal to True. This is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $true -and $false<br \/> False<br \/> PS C:\\&gt; $true -and $true<br \/> True<br \/> PS C:\\&gt; $false -and $false<br \/> False<br \/> PS C:\\&gt;<\/p>\n<p>Following is the revised <strong>Get-AllowedComputer<\/strong> function:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Function Get-AllowedComputer([string]$computer, [string]$property)<br \/> {<br \/> &nbsp;$servers = Get-Content -path c:\\fso\\serversAndProperties.txt <br \/> &nbsp;$s = $servers -contains $computer<br \/> &nbsp;$p = $servers -contains $property<br \/> &nbsp;Return $s -and $p<br \/> } #end Get-AllowedComputer function<\/p>\n<p>The <strong>if<\/strong> statement is used to determine if both the <strong>computer<\/strong> value and the <strong>property<\/strong> value are contained in the allowed list of servers and properties. If the <strong>Get-AllowedComputer<\/strong> function returns True, the <strong>Get-WmiObject<\/strong> cmdlet is used to display the chosen <strong>property<\/strong> value from the selected computer. This is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">if(Get-AllowedComputer -computer $computer -property $property)<br \/> &nbsp;{<br \/> &nbsp;&nbsp; Get-WmiObject -class Win32_Bios -Computer $computer | <br \/> &nbsp;&nbsp; Select-Object -property $property<br \/> &nbsp;}<\/p>\n<p>If the <strong>computer<\/strong> value and the <strong>property<\/strong> value are not on the permitted list, the Get-AllowedComputerAndProperty.ps1 script displays a message that states there is a non-permitted value:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Else<br \/> &nbsp;{<br \/> &nbsp; &#8220;Either $computer is not an allowed computer, `r`nor $property is not an allowed property&#8221;<br \/> &nbsp;}<\/p>\n<p>The complete Get-AllowedComputerAndProperty.ps1 script is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\"><strong>Get-AllowedComputerAndProperty.ps1<br \/> <\/strong>Param([string]$computer = $env:computername,[string]$property=&#8221;name&#8221;)<\/p>\n<p> Function Get-AllowedComputer([string]$computer, [string]$property)<br \/> {<br \/> &nbsp;$servers = Get-Content -path c:\\fso\\serversAndProperties.txt <br \/> &nbsp;$s = $servers -contains $computer<br \/> &nbsp;$p = $servers -contains $property<br \/> &nbsp;Return $s -and $p<br \/> } #end Get-AllowedComputer function<\/p>\n<p> # *** Entry point to Script ***<\/p>\n<p> if(Get-AllowedComputer -computer $computer -property $property)<br \/> &nbsp;{<br \/> &nbsp;&nbsp; Get-WmiObject -class Win32_Bios -Computer $computer | <br \/> &nbsp;&nbsp; Select-Object -property $property<br \/> &nbsp;}<br \/> Else<br \/> &nbsp;{<br \/> &nbsp; &#8220;Either $computer is not an allowed computer, `r`nor $property is not an allowed property&#8221;<br \/> &nbsp;}<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: &nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Contains operator to work with arrays. Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book Windows PowerShell 3.0 Step by Step, published by Microsoft Press. Examine contents of an array To verify [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-2431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: &nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Contains operator to work with arrays. Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book Windows PowerShell 3.0 Step by Step, published by Microsoft Press. Examine contents of an array To verify [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2431"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}