{"id":8731,"date":"2012-07-13T00:01:00","date_gmt":"2012-07-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/07\/13\/use-the-like-operator-to-simplify-your-wql-queries\/"},"modified":"2012-07-13T00:01:00","modified_gmt":"2012-07-13T00:01:00","slug":"use-the-like-operator-to-simplify-your-wql-queries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-like-operator-to-simplify-your-wql-queries\/","title":{"rendered":"Use the Like Operator to Simplify Your WQL Queries"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to simplify your WQL queries by using the <b>like<\/b> operator and special wildcard characters.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> Hey, Scripting Guy! OK, I can see that using WMI queries might be cool. But I do not like having to always specify exact properties and values. Is it possible to use wildcard characters for some of this stuff?&rdquo;<\/p>\n<p>&mdash;SC<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> Hello SC,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife was rummaging around, and she decided to use some of our hotel points to go to Myrtle Beach, South Carolina for the weekend. I guess she did not think it was hot enough in Charlotte, North Carolina. I said, &ldquo;OK,&rdquo; because basically, I am going to be working on the finishing touches for my TechReady presentation in Seattle. I find working while I am looking out the window at the beach relaxing, and I can feel the stress ebbing away with each passing wave. The cool thing about Windows Server 2012 is that is has the real Hyper-V built into it, so I can run multiple virtual machines on my laptop. Sweet!<\/p>\n<p>So, SC, you want to use some wildcards in your WQL queries&hellip;<\/p>\n<h2>Using the like operator<\/h2>\n<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p><b>Symbol<\/b><\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p><b>Meaning<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>[ ]<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>Character in a range [a-f] or a set of characters [abcdef]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>^<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>Character not in a range [^a-f] or not in a set [^abcdef]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>%<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>Any string of zero or more characters<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"319\" valign=\"top\">\n<p>_ (underscore character)<\/p>\n<\/td>\n<td width=\"319\" valign=\"top\">\n<p>Any one character. <br \/> <b>Note&nbsp;&nbsp;<\/b>A literal underscore used in the query string must&nbsp; be escaped by placing it inside square brackets [_].&nbsp;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Use like in a WQL query and look for a range<\/h2>\n<p>To use the <b>like<\/b> operator in a WQL query and look for a range of characters, use the square brackets. In the following example, the notepad<i> <\/i>process starts. Next, a WQL query is created that uses the <b>like<\/b> operator and the range. A range of letters from H through N is created by using the WQL range characters [H-N]. The square brackets, are placed inside the string that <b>like<\/b> uses in the search. Here any process from hotpad.exe through notepad.exe will be returned by the query. &nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; notepad<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $query = &#8220;Select * from win32_Process where name like &#8216;[H-N]otepad.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject -Query $query | select name, processID<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1740<\/p>\n<p>You can combine the range operation with the percent wildcard character to create simple, yet powerful filters. In the code that follows, everything is selected from the <b>Win32_Process<\/b> WMI class where the name begins with a letter in the range from A &ndash; P, and is followed by zero or more letters in any combination. The <b>Get-WmiObject<\/b> cmdlet executes the query, and chooses only the name and the process ID. The output is sorted by name. The code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;Select * from win32_Process where name like &#8216;[A-P]%'&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $query | select name, processID | sort name<\/p>\n<p>The code and the output associated with the commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2821.HSG-7-13-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2821.HSG-7-13-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Use like in a WQL query and look for characters not in a range<\/h2>\n<p>Sometimes you know what you do not want to know, but you are not certain what you really want to know. The <b>not in a range<\/b><i> <\/i>character is the caret (<b>^<\/b>). You do not need to create a consecutive range of characters. In fact, you do not need to even supply the characters in order. In the following query, no process that&nbsp; begins with one of the letters ASWPRCU or N followed by any other letter will be retrieved. The <b>Get-WmiObject<\/b> cmdlet is used to execute the query, and the name and processed properties are selected. Next they are sorted by name. &nbsp;<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;Select * from win32_Process where name like &#8216;[^ASWPRCUN]%'&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $query | select name, processID | sort name<\/p>\n<p>The output appearing in the following image illustrates running this query and the output associated with the query.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8688.HSG-7-13-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8688.HSG-7-13-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Use a wildcard character with your WQL query<\/h2>\n<p>The most useful character to use with the <b>like<\/b> operator is the percent character &ldquo;<b>%<\/b>.&rdquo; The percent character will match zero or more characters. It is equivalent to the asterisk &ldquo;<b>*<\/b>&rdquo; character. In the following example, a WQL query first looks for a process named calculator.exe, but nothing returns from the query. Next, the wildcard character is used and the WMI query returns the requested data.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $query = &#8220;Select * from win32_Process where name like &#8216;calculator.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject -Query $query | select name, processID<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $query = &#8220;Select * from win32_Process where name like &#8216;calc%'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject -Query $query | select name, processID<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4424<\/p>\n<h2>Using a single character wildcard with WQL<\/h2>\n<p>Sometimes, you want a very focused filter. In the query shown here, any process named anything from calc.exe through czlc.exe will match, in addition to other characters that may appear in process names.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $query = &#8220;Select * from win32_Process where name like &#8216;c_lc.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject -Query $query | select name, processID<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; processID<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">calc.exe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4424<\/p>\n<p>In the following query, the process with the name WLIDSVC.exe produces one match.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>When using the <b>like<\/b> operator without any wildcards or range operators, you basically have an equality operation; and therefore, you will only get an exact match.<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;Select * from win32_Process where name <b>like<\/b> &#8216;WLIDSVC.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $query | select name, processID<\/p>\n<p>By using the underscore character for a one-letter wildcard <b>like<\/b> operation, you do not match the WLIDSVC.exe process, but rather you match the WLIDSVCM.exe process.<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;Select * from win32_Process where name <b>like<\/b> &#8216;WLIDSVC_.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $query | select name, processID<\/p>\n<p>By using the percentage sign wildcard character, you match both of the processes.<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;Select * from win32_Process where name <b>like<\/b> &#8216;WLIDSVC%.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $query | select name, processID<\/p>\n<p>The three queries and the resulting output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4024.HSG-7-13-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4024.HSG-7-13-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>SC, that is all there is to using the <b>like<\/b> operator in a WQL query. WMI Week will continue tomorrow when I will talk some more about performing WMI data queries.<\/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><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to simplify your WQL queries by using the like operator and special wildcard characters. Hey, Scripting Guy! OK, I can see that using WMI queries might be cool. But I do not like having to always specify exact properties and values. Is it possible to use wildcard characters for some of this [&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":[3,4,45,6],"class_list":["post-8731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to simplify your WQL queries by using the like operator and special wildcard characters. Hey, Scripting Guy! OK, I can see that using WMI queries might be cool. But I do not like having to always specify exact properties and values. Is it possible to use wildcard characters for some of this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8731","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=8731"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8731\/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=8731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}