{"id":55123,"date":"2008-09-03T02:19:00","date_gmt":"2008-09-03T02:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/09\/03\/hey-scripting-guy-how-do-i-use-windows-powershell-to-run-the-same-wmi-queries-against-multiple-computers\/"},"modified":"2008-09-03T02:19:00","modified_gmt":"2008-09-03T02:19:00","slug":"hey-scripting-guy-how-do-i-use-windows-powershell-to-run-the-same-wmi-queries-against-multiple-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-do-i-use-windows-powershell-to-run-the-same-wmi-queries-against-multiple-computers\/","title":{"rendered":"Hey, Scripting Guy! How Do I Use Windows PowerShell to Run the Same WMI Queries Against Multiple Computers?"},"content":{"rendered":"<p><span class=\"Apple-style-span\"><\/p>\n<h2><span class=\"Apple-style-span\"><\/p>\n<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/h2>\n<p>Hey, Scripting Guy! I need to run the same WMI queries against multiple computers on our network. Can I do this with Windows PowerShell?<\/p>\n<p>&#8211; KB<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hi KB,<\/p>\n<p>Not only can you do this with Windows PowerShell, but you will be surprised at how easy it is to do. If you can create a text file in Notepad, you can run your multiple queries against multiple servers. Well, actually, you need two text files and a script, but other than that it is really simple. Here is the&nbsp;code:<\/p>\n<pre class=\"codeSample\">$aryquery = Get-Content -path c:\\fso\\query.txt\n$computer = Get-Content -path c:\\fso\\servers.txt\nForeach ($query in $aryQuery)\n{\n Get-WmiObject -Query $query -computername $computer\n}\n<\/pre>\n<p>The script begins by reading two separate text files and storing the results into an array of text strings. It then uses a&nbsp;<b>ForEach<\/b>statement to walk through each of the queries, and then it performs the query.<\/p>\n<p>To read the text files we use the&nbsp;<b>Get-Content<\/b>&nbsp;cmdlet and specify the path to the text file. The first text file we read stores the queries we intend to run.&nbsp;<b>Query.txt<\/b>&nbsp;is seen here:<\/p>\n<p><img decoding=\"async\" height=\"160\" alt=\"query.txt image\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/querytext.jpg\" width=\"500\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the&nbsp;<b>query.txt<\/b>&nbsp;file is nothing really special. It is simply a collection of WMI queries. As an added bonus, you do not even have to surround the queries in quotation marks, and you don&#8217;t need to worry about case-sensitivity issues. The syntax of these queries is exactly the same as you would have used when writing VBScripts. In fact, if it were me (wait, it&nbsp;<i>is<\/i>&nbsp;me), I would steal some queries from some of my old VBScripts and avoid reinventing the wheel (or in this case the query). You can see the query I stole from my own BLOCKED SCRIPT<\/p>\n<p><img decoding=\"async\" height=\"396\" alt=\"VBScript image\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/bios_vbs.jpg\" width=\"500\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>The next file we need to use is the&nbsp;<b>servers.txt<\/b>&nbsp;file. This file will contain the names of the servers we wish to query. As seen here,<b>servers.txt<\/b>&nbsp;file is nothing special either. It is literally a list of server names. No commas are required and neither are quotation marks. By the way, IP addresses would also work in this file:<\/p>\n<p><img decoding=\"async\" height=\"166\" alt=\"servers.txt image\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/serverstext.jpg\" width=\"500\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>With our two files out of the way, it is time to begin processing the files. This is a fancy word for &#8220;opening them up and taking a gander.&#8221; And we don&#8217;t mean stealing geese. We use&nbsp;<b>Get-Content<\/b>&nbsp;to read the content of the text files. Because there are two files, we will be using the&nbsp;<b>Get-Content<\/b>&nbsp;cmdlet twice, as seen&nbsp;here: <\/p>\n<pre class=\"codeSample\">$aryquery = Get-Content -path c:\\fso\\query.txt\n$computer = Get-Content -path c:\\fso\\servers.txt\n<\/pre>\n<p>We store the content of the query.txt file in the variable&nbsp;<b>$aryquery<\/b>, and we store the contents of the&nbsp;<b>server.txt<\/b>&nbsp;file in the&nbsp;<b>$computer<\/b>variable. You may be wondering why I named one variable&nbsp;<b>$aryquery<\/b>&nbsp;and the other&nbsp;<b>$computer<\/b>. Patience, scripthopper. You will understand this soon. When we use&nbsp;<b>Get-Content<\/b>&nbsp;it always returns an array. In VBScript, when you hear the word &#8220;array,&#8221; you think<b>FOR EACH NEXT<\/b>. Well, in Windows PowerShell, when you hear the word &#8220;array,&#8221; you should think&nbsp;<b>ForEach<\/b>. Because we have an array of WMI queries, we will need to use the&nbsp;<b>ForEach<\/b>&nbsp;statement to walk through our array of queries. As we go through the array, we will execute one query at a time. We use the variable&nbsp;<b>$query<\/b>&nbsp;to hold the individual query from our array. This is seen here:<\/p>\n<pre class=\"codeSample\">ForEach ($query in $aryQuery)<\/pre>\n<p>To execute the WMI queries, we will use the&nbsp;<b>Get-WmiObject<\/b>&nbsp;cmdlet. We use the&nbsp;<b>-query<\/b>&nbsp;parameter to supply the query we wish to execute. Now here is where things really get cool. The&nbsp;<b>-computername<\/b>&nbsp;parameter in the&nbsp;<b>Get-WmiObject<\/b>&nbsp;cmdlet can take an array! You read this right! It will take an array! That&#8217;s three exclamation points! Now four! Okay, I&#8217;ll stop. This is why I did not bother to name the contents of the&nbsp;<b>servers.txt<\/b>&nbsp;file with the&nbsp;<b>ary<\/b>&nbsp;prefix. I had no intention of iterating through the array. This line of code is seen here:<\/p>\n<pre class=\"codeSample\">Get-WmiObject -Query $query -computername $computer<\/pre>\n<p>When you run this script, it could produce a lot of output, and you may want to write the contents to a file. You could use either the redirection arrows (&gt;&gt;) or you could use&nbsp;<b>out-file<\/b>. If you used the &gt;&gt; arrows it would look something like this:<\/p>\n<pre class=\"codeSample\">Get-WmiObject -Query $query -computername $computer &gt;&gt; c:\\fso\\output.txt<\/pre>\n<p>(If you have a folder named&nbsp;<b>fso<\/b>&nbsp;and are writing to a file named&nbsp;<b>output.txt<\/b>, that&#8217;s exactly how the command will look; otherwise, your command will look different.)<\/p>\n<p>So, KB, this is it. This could be the last WMI script you ever need to write. From here on out, all you need to do is to copy the WMI queries from your old VBScripts and paste them into the&nbsp;<b>query.txt<\/b>&nbsp;file and you are done. Of course, this probably will not be the last WMI script you ever write, because WMI is fun stuff.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/span><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><\/font><\/h2>\n<p><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to run the same WMI queries against multiple computers on our network. Can I do this with Windows PowerShell? &#8211; KB Hi KB, Not only can you do this with Windows PowerShell, but you will be surprised at how easy it is to do. If you can create a text [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,14,45,6],"class_list":["post-55123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to run the same WMI queries against multiple computers on our network. Can I do this with Windows PowerShell? &#8211; KB Hi KB, Not only can you do this with Windows PowerShell, but you will be surprised at how easy it is to do. If you can create a text [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55123","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55123\/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=55123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}