{"id":4094,"date":"2013-02-28T00:01:00","date_gmt":"2013-02-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/28\/use-powershell-to-explore-process-threads-in-windows\/"},"modified":"2013-02-28T00:01:00","modified_gmt":"2013-02-28T00:01:00","slug":"use-powershell-to-explore-process-threads-in-windows","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-explore-process-threads-in-windows\/","title":{"rendered":"Use PowerShell to Explore Process Threads in Windows"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy talks about using Windows PowerShell to explore process threads in Windows.<\/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\" \/>&nbsp;Hey, Scripting Guy! I have a problem. On our system, every once in a while, we have this application where the threads go crazy. I need an easy way to check threads. Can you help?<\/p>\n<p>&mdash;BC<\/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\" \/>&nbsp;Hello BC,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well it is official; there will be a Microsoft Scripting Guy booth at <a href=\"http:\/\/northamerica.msteched.com\/#fbid=zHBiru6mqIA\" target=\"_blank\">TechEd 2013 in New Orleans<\/a>. The Scripting Wife will also be at the booth. We are planning to share our booth with the Windows PowerShell community from PowerShell.org as well. It will be a lot of fun, and we are already looking forward to it. The dates for TechEd 2013 in New Orleans, by the way, are June 3 &ndash; June 6.<\/p>\n<h2>Use WMI to find info about threads<\/h2>\n<p>To find information about threads, I use the Win32_Thread WMI class. I found this by using the <strong>Get-CimClass<\/strong> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimClass *thread*<\/p>\n<p>The command and its associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1108.HSG-2-28-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1108.HSG-2-28-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I can also do the same thing by using the <strong>Get-WmiObject<\/strong> cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-wmiobject -list *thread*<\/p>\n<p>So, I decide to query the WMI class. Here is the Windows PowerShell&nbsp;2.0 version of the command.<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_thread<\/p>\n<p>I can do the same thing with the CIM cmdlets in Windows PowerShell&nbsp;3.0. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance win32_thread<\/p>\n<p>The command and the output from the command are shown here.<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1588.HSG-2-28-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1588.HSG-2-28-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p><span style=\"font-size: 1.5em\">Find a specific thread<\/span><\/p>\n<p>The easiest way to find a specific thread is to first get the process handle, and then use that handle in a WMI filter. The following command obtains the handle for a running instance of Notepad, and then obtains the thread information.<\/p>\n<p style=\"padding-left: 30px\">$handle = (Get-Process notepad).handle<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_thread -filter &#8220;handle = $handle&#8221;<\/p>\n<p>By using the <strong>Get-CimInstance<\/strong> Windows PowerShell&nbsp;3.0 CIM cmdlet, I arrive at the following syntax.<\/p>\n<p style=\"padding-left: 30px\">$handle = (Get-Process notepad).handle<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance win32_thread -filter &#8220;handle = $handle&#8221;<\/p>\n<p>There is very little difference between the two commands. There is a bit of a difference between the output from the two commands. The output from the <strong>Get-CimInstance<\/strong> cmdlet is cleaner. The command and output from <strong>Get-CimInstance<\/strong> is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.HSG-2-28-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.HSG-2-28-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To understand the thread state, it is necessary to look up the <strong>ThreadState<\/strong> property. I can do this in the MSDN article, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa394494(v=vs.85).aspx\" target=\"_blank\">Win32_Thread WMI class<\/a>. The <strong>ThreadState <\/strong>values are shown here.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\"><strong>Value<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p align=\"center\"><strong>Meaning<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">0<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Initialized. It is recognized by the microkernel.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">1<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Ready. It is prepared to run on the next available processor.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">2<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Running. It is executing.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">3<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Standby. It is about to run. Only one thread may be in this state at a time.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">4<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Terminated. It is finished executing.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">5<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Waiting. It is not ready for the processor. When ready, it will be rescheduled.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">6<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Transition. The thread is waiting for resources other than the processor.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"73\">\n<p align=\"center\">7<\/p>\n<\/td>\n<td valign=\"top\" width=\"480\">\n<p>&nbsp; Unknown. The thread state is unknown.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <strong>ThreadWaitReason <\/strong>value codes are shown in the table that follows.&nbsp;<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\"><strong>Value<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\"><strong>Meaning<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">0<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">Executive<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">1<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">FreePage<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">2<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PageIn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">3<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PoolAllocation<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">4<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">ExecutionDelay<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">5<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">FreePage<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">6<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PageIn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">7<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">Executive<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">8<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">FreePage<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">9<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PageIn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">10<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PoolAllocation<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">11<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">ExecutionDelay<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">12<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">FreePage<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">13<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PageIn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">14<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">EventPairHigh<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">15<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">EventPairLow<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">16<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">LPCReceive<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">17<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">LPCReply<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">18<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">VirtualMemory<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">19<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">PageOut<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"151\">\n<p align=\"center\">20<\/p>\n<\/td>\n<td valign=\"top\" width=\"288\">\n<p align=\"center\">Unknown<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>BC, that is all there is to using Windows PowerShell and WMI to find information about threads. Join me tomorrow when I will talk about more cool stuff.Therefore, the Notepad process is waiting and not ready for the processor. The reason it is waiting is <strong>EventPairLow<\/strong>.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy talks about using Windows PowerShell to explore process threads in Windows. &nbsp;Hey, Scripting Guy! I have a problem. On our system, every once in a while, we have this application where the threads go crazy. I need an easy way to check threads. Can you help? &mdash;BC &nbsp;Hello BC, Microsoft Scripting [&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":[31,87,3,4,45,6],"class_list":["post-4094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy talks about using Windows PowerShell to explore process threads in Windows. &nbsp;Hey, Scripting Guy! I have a problem. On our system, every once in a while, we have this application where the threads go crazy. I need an easy way to check threads. Can you help? &mdash;BC &nbsp;Hello BC, Microsoft Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4094","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=4094"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4094\/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=4094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}