The Win32_ThreadâWMI class represents a thread of execution. While a process must have one thread of execution, the process can create other threads to execute tasks in parallel. Threads share the process environment, thus multiple threads under the same process use less memory than the same number of processes.
Methods
Win32_Thread has no methods.
Properties
Win32_Thread returns 22 properties:
'Caption','CreationClassName','CSCreationClassName','CSName','Description',
'ElapsedTime','ExecutionState','Handle','InstallDate','KernelModeTime','Name','OSCreationClassName',
'OSName','Priority','PriorityBase','ProcessCreationClassName','ProcessHandle','StartAddress',
'Status','ThreadState','ThreadWaitReason','UserModeTime'
Unless explicitly marked as writeable, all properties are read-only. Read all properties for all instances:
Get-CimInstance -ClassName Win32_Thread -Property *
Most WMI classes return one or more instances.
When
Get-CimInstance
returns no result, then apparently no instances of class Win32_Thread exist. This is normal behavior.Either the class is not implemented on your system (may be deprecated or due to missing drivers, i.e. CIM_VideoControllerResolution), or there are simply no physical representations of this class currently available (i.e. Win32_TapeDrive).
Caption
Short description of the object.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Caption
CreationClassName
Name of the first concrete class to appear in the inheritance chain used in the creation of an instance. When used with the other key properties of the class, this property allows all instances of this class and its subclasses to be uniquely identified.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property CreationClassName
CSCreationClassName
Creation class name of the scoping computer system.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property CSCreationClassName
CSName
Name of the scoping computer system.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property CSName
Description
Description of the object.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Description
ElapsedTime
Total execution time, in milliseconds, given to this thread since its creation.
For more information about using uint64 values in scripts, see Scripting in WMI.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property ElapsedTime
ExecutionState
Current operating condition of the thread.
ExecutionState returns a numeric value. To translate it into a meaningful text, use any of the following approaches:
Use a PowerShell Hashtable
$ExecutionState_map = @{
0 = 'Unknown'
1 = 'Other'
2 = 'Ready'
3 = 'Running'
4 = 'Blocked'
5 = 'Suspended Blocked'
6 = 'Suspended Ready'
}
Use a switch statement
switch([int]$value)
{
0 {'Unknown'}
1 {'Other'}
2 {'Ready'}
3 {'Running'}
4 {'Blocked'}
5 {'Suspended Blocked'}
6 {'Suspended Ready'}
default {"$value"}
}
Use Enum structure
Enum EnumExecutionState
{
Unknown = 0
Other = 1
Ready = 2
Running = 3
Blocked = 4
Suspended_Blocked = 5
Suspended_Ready = 6
}
Examples
Use $ExecutionState_map in a calculated property for Select-Object
<#
this example uses a hashtable to translate raw numeric values for
property "ExecutionState" to friendly text
Note: to use other properties than "ExecutionState", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "ExecutionState"
# to translate other properties, use their translation table instead
$ExecutionState_map = @{
0 = 'Unknown'
1 = 'Other'
2 = 'Ready'
3 = 'Running'
4 = 'Blocked'
5 = 'Suspended Blocked'
6 = 'Suspended Ready'
}
#endregion define hashtable
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ExecutionState", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ExecutionState = @{
Name = 'ExecutionState'
Expression = {
# property is an array, so process all values
$value = $_.ExecutionState
$ExecutionState_map[[int]$value]
}
}
#endregion define calculated property
# retrieve the instances, and output the properties "Caption" and "ExecutionState". The latter
# is defined by the hashtable in $ExecutionState:
Get-CimInstance -Class Win32_Thread | Select-Object -Property Caption, $ExecutionState
# ...or dump content of property ExecutionState:
$friendlyValues = Get-CimInstance -Class Win32_Thread |
Select-Object -Property $ExecutionState |
Select-Object -ExpandProperty ExecutionState
# output values
$friendlyValues
# output values as comma separated list
$friendlyValues -join ', '
# output values as bullet list
$friendlyValues | ForEach-Object { "- $_" }
Use $ExecutionState_map to directly translate raw values from an instance
<#
this example uses a hashtable to manually translate raw numeric values
for property "Win32_Thread" to friendly text. This approach is ideal when
there is just one instance to work with.
Note: to use other properties than "Win32_Thread", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "Win32_Thread"
# to translate other properties, use their translation table instead
$ExecutionState_map = @{
0 = 'Unknown'
1 = 'Other'
2 = 'Ready'
3 = 'Running'
4 = 'Blocked'
5 = 'Suspended Blocked'
6 = 'Suspended Ready'
}
#endregion define hashtable
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to illustrate
the number-to-text translation. To process all instances, replace
"Select-Object -First 1" with a "Foreach-Object" loop, and use
the iterator variable $_ instead of $instance
#>
# query the property
$rawValue = $instance.ExecutionState
# translate raw value to friendly text:
$friendlyName = $ExecutionState_map[[int]$rawValue]
# output value
$friendlyName
Use a switch statement inside a calculated property for Select-Object
<#
this example uses a switch clause to translate raw numeric
values for property "ExecutionState" to friendly text. The switch
clause is embedded into a calculated property so there is
no need to refer to external variables for translation.
Note: to use other properties than "ExecutionState", look up the appropriate
translation switch clause for the property you would like to use instead.
#>
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ExecutionState", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ExecutionState = @{
Name = 'ExecutionState'
Expression = {
# property is an array, so process all values
$value = $_.ExecutionState
switch([int]$value)
{
0 {'Unknown'}
1 {'Other'}
2 {'Ready'}
3 {'Running'}
4 {'Blocked'}
5 {'Suspended Blocked'}
6 {'Suspended Ready'}
default {"$value"}
}
}
}
#endregion define calculated property
# retrieve all instances...
Get-CimInstance -ClassName Win32_Thread |
# ...and output properties "Caption" and "ExecutionState". The latter is defined
# by the hashtable in $ExecutionState:
Select-Object -Property Caption, $ExecutionState
Use the Enum from above to auto-translate the code values
<#
this example translates raw values by means of type conversion
the friendly names are defined as enumeration using the
keyword "enum" (PowerShell 5 or better)
The raw value(s) are translated to friendly text by
simply converting them into the enum type.
Note: to use other properties than "Win32_Thread", look up the appropriate
enum definition for the property you would like to use instead.
#>
#region define enum with value-to-text translation:
Enum EnumExecutionState
{
Unknown = 0
Other = 1
Ready = 2
Running = 3
Blocked = 4
Suspended_Blocked = 5
Suspended_Ready = 6
}
#endregion define enum
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to focus on
the number-to-text type conversion.
To process all instances, replace "Select-Object -First 1"
with a "Foreach-Object" loop, and use the iterator variable
$_ instead of $instance
#>
# query the property:
$rawValue = $instance.ExecutionState
#region using strict type conversion
<#
Note: strict type conversion fails if the raw value is
not defined by the enum. So if the list of allowable values
was extended and the enum does not match the value,
an exception is thrown
#>
# convert the property to the enum **ExecutionState**
[EnumExecutionState]$rawValue
# get a comma-separated string:
[EnumExecutionState]$rawValue -join ','
#endregion
#region using operator "-as"
<#
Note: the operator "-as" accepts values not defined
by the enum and returns $null instead of throwing
an exception
#>
$rawValue -as [EnumExecutionState]
#endregion
Enums must cover all possible values. If ExecutionState returns a value that is not defined in the enum, an exception occurs. The exception reports the value that was missing in the enum. To fix, add the missing value to the enum.
Handle
Handle to a thread. The handle has full access rights by default. With the correct security access, the handle can be used in any function that accepts a thread handle. Depending on the inheritance flag specified when it is created, this handle can be inherited by child processes.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Handle
InstallDate
Object was installed. This property does not need a value to indicate that the object is installed.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property InstallDate
KernelModeTime
Time in kernel mode, in 100 nanosecond units. If this information is not available, a value of 0 (zero) should be used.
For more information about using uint64 values in scripts, see Scripting in WMI.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property KernelModeTime
Name
Label by which the object is known. When subclassed, the property can be overridden to be a key property.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Name
OSCreationClassName
Creation class name of the scoping operating system.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property OSCreationClassName
OSName
Name of the scoping operating system.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property OSName
Priority
Dynamic priority of the thread. Each thread has a dynamic priority that the scheduler uses to determine which thread to execute. Initially, a thread’s dynamic priority is the same as its base priority. The system can raise and lower the dynamic priority, to ensure that it is responsive (guaranteeing that no threads are starved for processor time). The system does not boost the priority of threads with a base priority level between 16 and 31. Only threads with a base priority between 0 and 15 receive dynamic priority boosts. Higher numbers indicate higher priorities.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Priority
PriorityBase
Current base priority of a thread. The operating system may raise the thread’s dynamic priority above the base priority if the thread is handling user input, or lower it toward the base priority if the thread becomes compute-bound. The PriorityBase property can have a value between 0 and 31.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property PriorityBase
ProcessCreationClassName
Value of the scoping process CreationClassName property.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property ProcessCreationClassName
ProcessHandle
Process that created the thread. The contents of this property can be used by Windows application programming interface (API) elements.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property ProcessHandle
StartAddress
Starting address of the thread. Because any application with appropriate access to the thread can change the thread’s context, this value may only be an approximation of the thread’s starting address.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property StartAddress
Status
Current status of an object. Various operational and nonoperational statuses can be defined. Available values:
$values = 'Degraded','Error','Lost Comm','No Contact','NonRecover','OK','Pred Fail','Service','Starting','Stopping','Stressed','Unknown'
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property Status
ThreadState
Current execution state for the thread.
ThreadState returns a numeric value. To translate it into a meaningful text, use any of the following approaches:
Use a PowerShell Hashtable
$ThreadState_map = @{
0 = 'Initialized'
1 = 'Ready'
2 = 'Running'
3 = 'Standby'
4 = 'Terminated'
5 = 'Waiting'
6 = 'Transition'
7 = 'Unknown'
}
Use a switch statement
switch([int]$value)
{
0 {'Initialized'}
1 {'Ready'}
2 {'Running'}
3 {'Standby'}
4 {'Terminated'}
5 {'Waiting'}
6 {'Transition'}
7 {'Unknown'}
default {"$value"}
}
Use Enum structure
Enum EnumThreadState
{
Initialized = 0
Ready = 1
Running = 2
Standby = 3
Terminated = 4
Waiting = 5
Transition = 6
Unknown = 7
}
Examples
Use $ThreadState_map in a calculated property for Select-Object
<#
this example uses a hashtable to translate raw numeric values for
property "ThreadState" to friendly text
Note: to use other properties than "ThreadState", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "ThreadState"
# to translate other properties, use their translation table instead
$ThreadState_map = @{
0 = 'Initialized'
1 = 'Ready'
2 = 'Running'
3 = 'Standby'
4 = 'Terminated'
5 = 'Waiting'
6 = 'Transition'
7 = 'Unknown'
}
#endregion define hashtable
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ThreadState", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ThreadState = @{
Name = 'ThreadState'
Expression = {
# property is an array, so process all values
$value = $_.ThreadState
$ThreadState_map[[int]$value]
}
}
#endregion define calculated property
# retrieve the instances, and output the properties "Caption" and "ThreadState". The latter
# is defined by the hashtable in $ThreadState:
Get-CimInstance -Class Win32_Thread | Select-Object -Property Caption, $ThreadState
# ...or dump content of property ThreadState:
$friendlyValues = Get-CimInstance -Class Win32_Thread |
Select-Object -Property $ThreadState |
Select-Object -ExpandProperty ThreadState
# output values
$friendlyValues
# output values as comma separated list
$friendlyValues -join ', '
# output values as bullet list
$friendlyValues | ForEach-Object { "- $_" }
Use $ThreadState_map to directly translate raw values from an instance
<#
this example uses a hashtable to manually translate raw numeric values
for property "Win32_Thread" to friendly text. This approach is ideal when
there is just one instance to work with.
Note: to use other properties than "Win32_Thread", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "Win32_Thread"
# to translate other properties, use their translation table instead
$ThreadState_map = @{
0 = 'Initialized'
1 = 'Ready'
2 = 'Running'
3 = 'Standby'
4 = 'Terminated'
5 = 'Waiting'
6 = 'Transition'
7 = 'Unknown'
}
#endregion define hashtable
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to illustrate
the number-to-text translation. To process all instances, replace
"Select-Object -First 1" with a "Foreach-Object" loop, and use
the iterator variable $_ instead of $instance
#>
# query the property
$rawValue = $instance.ThreadState
# translate raw value to friendly text:
$friendlyName = $ThreadState_map[[int]$rawValue]
# output value
$friendlyName
Use a switch statement inside a calculated property for Select-Object
<#
this example uses a switch clause to translate raw numeric
values for property "ThreadState" to friendly text. The switch
clause is embedded into a calculated property so there is
no need to refer to external variables for translation.
Note: to use other properties than "ThreadState", look up the appropriate
translation switch clause for the property you would like to use instead.
#>
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ThreadState", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ThreadState = @{
Name = 'ThreadState'
Expression = {
# property is an array, so process all values
$value = $_.ThreadState
switch([int]$value)
{
0 {'Initialized'}
1 {'Ready'}
2 {'Running'}
3 {'Standby'}
4 {'Terminated'}
5 {'Waiting'}
6 {'Transition'}
7 {'Unknown'}
default {"$value"}
}
}
}
#endregion define calculated property
# retrieve all instances...
Get-CimInstance -ClassName Win32_Thread |
# ...and output properties "Caption" and "ThreadState". The latter is defined
# by the hashtable in $ThreadState:
Select-Object -Property Caption, $ThreadState
Use the Enum from above to auto-translate the code values
<#
this example translates raw values by means of type conversion
the friendly names are defined as enumeration using the
keyword "enum" (PowerShell 5 or better)
The raw value(s) are translated to friendly text by
simply converting them into the enum type.
Note: to use other properties than "Win32_Thread", look up the appropriate
enum definition for the property you would like to use instead.
#>
#region define enum with value-to-text translation:
Enum EnumThreadState
{
Initialized = 0
Ready = 1
Running = 2
Standby = 3
Terminated = 4
Waiting = 5
Transition = 6
Unknown = 7
}
#endregion define enum
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to focus on
the number-to-text type conversion.
To process all instances, replace "Select-Object -First 1"
with a "Foreach-Object" loop, and use the iterator variable
$_ instead of $instance
#>
# query the property:
$rawValue = $instance.ThreadState
#region using strict type conversion
<#
Note: strict type conversion fails if the raw value is
not defined by the enum. So if the list of allowable values
was extended and the enum does not match the value,
an exception is thrown
#>
# convert the property to the enum **ThreadState**
[EnumThreadState]$rawValue
# get a comma-separated string:
[EnumThreadState]$rawValue -join ','
#endregion
#region using operator "-as"
<#
Note: the operator "-as" accepts values not defined
by the enum and returns $null instead of throwing
an exception
#>
$rawValue -as [EnumThreadState]
#endregion
Enums must cover all possible values. If ThreadState returns a value that is not defined in the enum, an exception occurs. The exception reports the value that was missing in the enum. To fix, add the missing value to the enum.
ThreadWaitReason
Reason why the thread is waiting. This value is only valid if the ThreadState member is set to Transition (6). Event pairs allow communication with protected subsystems.
ThreadWaitReason returns a numeric value. To translate it into a meaningful text, use any of the following approaches:
Use a PowerShell Hashtable
$ThreadWaitReason_map = @{
0 = 'Executive'
1 = 'FreePage'
2 = 'PageIn'
3 = 'PoolAllocation'
4 = 'ExecutionDelay'
5 = 'FreePage'
6 = 'PageIn'
7 = 'Executive'
8 = 'FreePage'
9 = 'PageIn'
10 = 'PoolAllocation'
11 = 'ExecutionDelay'
12 = 'FreePage'
13 = 'PageIn'
14 = 'EventPairHigh'
15 = 'EventPairLow'
16 = 'LPCReceive'
17 = 'LPCReply'
18 = 'VirtualMemory'
19 = 'PageOut'
20 = 'Unknown'
}
Use a switch statement
switch([int]$value)
{
0 {'Executive'}
1 {'FreePage'}
2 {'PageIn'}
3 {'PoolAllocation'}
4 {'ExecutionDelay'}
5 {'FreePage'}
6 {'PageIn'}
7 {'Executive'}
8 {'FreePage'}
9 {'PageIn'}
10 {'PoolAllocation'}
11 {'ExecutionDelay'}
12 {'FreePage'}
13 {'PageIn'}
14 {'EventPairHigh'}
15 {'EventPairLow'}
16 {'LPCReceive'}
17 {'LPCReply'}
18 {'VirtualMemory'}
19 {'PageOut'}
20 {'Unknown'}
default {"$value"}
}
Use Enum structure
Enum EnumThreadWaitReason
{
Executive1 = 0
FreePage1 = 1
PageIn1 = 2
PoolAllocation1 = 3
ExecutionDelay1 = 4
FreePage2 = 5
PageIn2 = 6
Executive2 = 7
FreePage3 = 8
PageIn3 = 9
PoolAllocation2 = 10
ExecutionDelay2 = 11
FreePage4 = 12
PageIn4 = 13
EventPairHigh = 14
EventPairLow = 15
LPCReceive = 16
LPCReply = 17
VirtualMemory = 18
PageOut = 19
Unknown = 20
}
Examples
Use $ThreadWaitReason_map in a calculated property for Select-Object
<#
this example uses a hashtable to translate raw numeric values for
property "ThreadWaitReason" to friendly text
Note: to use other properties than "ThreadWaitReason", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "ThreadWaitReason"
# to translate other properties, use their translation table instead
$ThreadWaitReason_map = @{
0 = 'Executive'
1 = 'FreePage'
2 = 'PageIn'
3 = 'PoolAllocation'
4 = 'ExecutionDelay'
5 = 'FreePage'
6 = 'PageIn'
7 = 'Executive'
8 = 'FreePage'
9 = 'PageIn'
10 = 'PoolAllocation'
11 = 'ExecutionDelay'
12 = 'FreePage'
13 = 'PageIn'
14 = 'EventPairHigh'
15 = 'EventPairLow'
16 = 'LPCReceive'
17 = 'LPCReply'
18 = 'VirtualMemory'
19 = 'PageOut'
20 = 'Unknown'
}
#endregion define hashtable
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ThreadWaitReason", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ThreadWaitReason = @{
Name = 'ThreadWaitReason'
Expression = {
# property is an array, so process all values
$value = $_.ThreadWaitReason
$ThreadWaitReason_map[[int]$value]
}
}
#endregion define calculated property
# retrieve the instances, and output the properties "Caption" and "ThreadWaitReason". The latter
# is defined by the hashtable in $ThreadWaitReason:
Get-CimInstance -Class Win32_Thread | Select-Object -Property Caption, $ThreadWaitReason
# ...or dump content of property ThreadWaitReason:
$friendlyValues = Get-CimInstance -Class Win32_Thread |
Select-Object -Property $ThreadWaitReason |
Select-Object -ExpandProperty ThreadWaitReason
# output values
$friendlyValues
# output values as comma separated list
$friendlyValues -join ', '
# output values as bullet list
$friendlyValues | ForEach-Object { "- $_" }
Use $ThreadWaitReason_map to directly translate raw values from an instance
<#
this example uses a hashtable to manually translate raw numeric values
for property "Win32_Thread" to friendly text. This approach is ideal when
there is just one instance to work with.
Note: to use other properties than "Win32_Thread", look up the appropriate
translation hashtable for the property you would like to use instead.
#>
#region define hashtable to translate raw values to friendly text
# Please note: this hashtable is specific for property "Win32_Thread"
# to translate other properties, use their translation table instead
$ThreadWaitReason_map = @{
0 = 'Executive'
1 = 'FreePage'
2 = 'PageIn'
3 = 'PoolAllocation'
4 = 'ExecutionDelay'
5 = 'FreePage'
6 = 'PageIn'
7 = 'Executive'
8 = 'FreePage'
9 = 'PageIn'
10 = 'PoolAllocation'
11 = 'ExecutionDelay'
12 = 'FreePage'
13 = 'PageIn'
14 = 'EventPairHigh'
15 = 'EventPairLow'
16 = 'LPCReceive'
17 = 'LPCReply'
18 = 'VirtualMemory'
19 = 'PageOut'
20 = 'Unknown'
}
#endregion define hashtable
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to illustrate
the number-to-text translation. To process all instances, replace
"Select-Object -First 1" with a "Foreach-Object" loop, and use
the iterator variable $_ instead of $instance
#>
# query the property
$rawValue = $instance.ThreadWaitReason
# translate raw value to friendly text:
$friendlyName = $ThreadWaitReason_map[[int]$rawValue]
# output value
$friendlyName
Use a switch statement inside a calculated property for Select-Object
<#
this example uses a switch clause to translate raw numeric
values for property "ThreadWaitReason" to friendly text. The switch
clause is embedded into a calculated property so there is
no need to refer to external variables for translation.
Note: to use other properties than "ThreadWaitReason", look up the appropriate
translation switch clause for the property you would like to use instead.
#>
#region define calculated property (to be used with Select-Object)
<#
a calculated property is defined by a hashtable with keys "Name" and "Expression"
"Name" defines the name of the property (in this example, it is "ThreadWaitReason", but you can rename it to anything else)
"Expression" defines a scriptblock that calculates the content of this property
in this example, the scriptblock uses the hashtable defined earlier to translate each numeric
value to its friendly text counterpart:
#>
$ThreadWaitReason = @{
Name = 'ThreadWaitReason'
Expression = {
# property is an array, so process all values
$value = $_.ThreadWaitReason
switch([int]$value)
{
0 {'Executive'}
1 {'FreePage'}
2 {'PageIn'}
3 {'PoolAllocation'}
4 {'ExecutionDelay'}
5 {'FreePage'}
6 {'PageIn'}
7 {'Executive'}
8 {'FreePage'}
9 {'PageIn'}
10 {'PoolAllocation'}
11 {'ExecutionDelay'}
12 {'FreePage'}
13 {'PageIn'}
14 {'EventPairHigh'}
15 {'EventPairLow'}
16 {'LPCReceive'}
17 {'LPCReply'}
18 {'VirtualMemory'}
19 {'PageOut'}
20 {'Unknown'}
default {"$value"}
}
}
}
#endregion define calculated property
# retrieve all instances...
Get-CimInstance -ClassName Win32_Thread |
# ...and output properties "Caption" and "ThreadWaitReason". The latter is defined
# by the hashtable in $ThreadWaitReason:
Select-Object -Property Caption, $ThreadWaitReason
Use the Enum from above to auto-translate the code values
<#
this example translates raw values by means of type conversion
the friendly names are defined as enumeration using the
keyword "enum" (PowerShell 5 or better)
The raw value(s) are translated to friendly text by
simply converting them into the enum type.
Note: to use other properties than "Win32_Thread", look up the appropriate
enum definition for the property you would like to use instead.
#>
#region define enum with value-to-text translation:
Enum EnumThreadWaitReason
{
Executive1 = 0
FreePage1 = 1
PageIn1 = 2
PoolAllocation1 = 3
ExecutionDelay1 = 4
FreePage2 = 5
PageIn2 = 6
Executive2 = 7
FreePage3 = 8
PageIn3 = 9
PoolAllocation2 = 10
ExecutionDelay2 = 11
FreePage4 = 12
PageIn4 = 13
EventPairHigh = 14
EventPairLow = 15
LPCReceive = 16
LPCReply = 17
VirtualMemory = 18
PageOut = 19
Unknown = 20
}
#endregion define enum
# get one instance:
$instance = Get-CimInstance -Class Win32_Thread | Select-Object -First 1
<#
IMPORTANT: this example processes only one instance to focus on
the number-to-text type conversion.
To process all instances, replace "Select-Object -First 1"
with a "Foreach-Object" loop, and use the iterator variable
$_ instead of $instance
#>
# query the property:
$rawValue = $instance.ThreadWaitReason
#region using strict type conversion
<#
Note: strict type conversion fails if the raw value is
not defined by the enum. So if the list of allowable values
was extended and the enum does not match the value,
an exception is thrown
#>
# convert the property to the enum **ThreadWaitReason**
[EnumThreadWaitReason]$rawValue
# get a comma-separated string:
[EnumThreadWaitReason]$rawValue -join ','
#endregion
#region using operator "-as"
<#
Note: the operator "-as" accepts values not defined
by the enum and returns $null instead of throwing
an exception
#>
$rawValue -as [EnumThreadWaitReason]
#endregion
Enums must cover all possible values. If ThreadWaitReason returns a value that is not defined in the enum, an exception occurs. The exception reports the value that was missing in the enum. To fix, add the missing value to the enum.
UserModeTime
Time in user mode, in 100 nanoseconds units. If this information is not available, a value of 0 (zero) should be used.
For more information about using uint64 values in scripts, see Scripting in WMI.
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property UserModeTime
Examples
List all instances of Win32_Thread
Get-CimInstance -ClassName Win32_Thread
Learn more about Get-CimInstance
and the deprecated Get-WmiObject
.
View all properties
Get-CimInstance -ClassName Win32_Thread -Property *
View key properties only
Get-CimInstance -ClassName Win32_Thread -KeyOnly
Selecting Properties
To select only some properties, pipe the results to Select-Object -Property a,b,c
with a comma-separated list of the properties you require. Wildcards are permitted.
Get-CimInstance
always returns all properties but only retrieves the ones that you specify. All other properties are empty but still present. That’s why you need to pipe the results into Select-Object
if you want to limit the visible properties, i.e. for reporting.
Selecting Properties
The code below lists all available properties. Remove the ones you do not need:
$properties = 'Caption',
'CreationClassName',
'CSCreationClassName',
'CSName',
'Description',
'ElapsedTime',
'ExecutionState',
'Handle',
'InstallDate',
'KernelModeTime',
'Name',
'OSCreationClassName',
'OSName',
'Priority',
'PriorityBase',
'ProcessCreationClassName',
'ProcessHandle',
'StartAddress',
'Status',
'ThreadState',
'ThreadWaitReason',
'UserModeTime'
Get-CimInstance -ClassName Win32_Thread | Select-Object -Property $properties
Limiting Network Bandwidth
If you work remotely, it makes sense to limit network bandwidth by filtering the properties on the server side, too:
Get-CimInstance -Class Win32_Thread -Property $property |
Select-Object -Property $property
Selecting Instances
To select some instances, use Get-CimInstance and a WMI Query. The wildcard character in WMI Queries is % (and not “*”).
The parameter -Filter runs a simple query.
Listing all instances where the property Caption starts with “A”
Get-CimInstance -Class Win32_Thread -Filter 'Caption LIKE "a%"'
Using a WQL Query
The parameter -Query uses a query similar to SQL and combines the parameters -Filter and -Property. This returns all instances where the property Caption starts with “A”, and returns the properties specified:
Get-CimInstance -Query "SELECT OSName, Handle, Caption, ExecutionState FROM Win32_Thread WHERE Caption LIKE 'a%'"
Any property you did not specify is still present but empty. You might need to use
Select-Object
to remove all unwanted properties:Get-CimInstance -Query "SELECT OSName, Handle, Caption, ExecutionState FROM Win32_Thread WHERE Caption LIKE 'a%'" | Select-Object -Property OSName, Handle, Caption, ExecutionState
Accessing Remote Computers
To access remote systems, you need to have proper permissions. User the parameter -ComputerName to access one or more remote systems.
Authenticating as Current User
# one or more computer names or IP addresses:
$list = 'server1', 'server2'
# authenticate with your current identity:
$result = Get-CimInstance -ClassName Win32_Thread -ComputerName $list
$result
Authenticating as Different User
Use a CIMSession object to authenticate with a new identity:
# one or more computer names or IP addresses:
$list = 'server1', 'server2'
# authenticate with a different identity:
$cred = Get-Credential -Message 'Authenticate to retrieve WMI information:'
$session = New-CimSession -ComputerName $list -Credential $cred
$result = Get-CimInstance Win32_Thread -CimSession $session
# remove the session after use (if you do not plan to re-use it later)
Remove-CimSession -CimSession $session
$result
Learn more about accessing remote computers.
Requirements
To use Win32_Thread, the following requirements apply:
PowerShell
Get-CimInstance
was introduced with PowerShell Version 3.0, which in turn was introduced on clients with Windows 8 and on servers with Windows Server 2012.
If necessary, update Windows PowerShell to Windows PowerShell 5.1, or install PowerShell 7 side-by-side.
Operating System
Win32_Thread was introduced on clients with Windows Vista and on servers with Windows Server 2008.
Namespace
Win32_Thread lives in the Namespace Root/CIMV2. This is the default namespace. There is no need to use the -Namespace parameter in Get-CimInstance
.
Implementation
Win32_Thread is implemented in CIMWin32.dll and defined in CIMWin32.mof. Both files are located in the folder C:\Windows\system32\wbem
:
explorer $env:windir\system32\wbem
notepad $env:windir\system32\wbem\CIMWin32.mof