Most modern computers have more than one processor, either physical or logical. If you’d like to find out the number of processors, here is a chunk of PowerShell code:

Get-WmiObject -Class Win32_Processor | 
  Select-Object -Property Caption, NumberOfLogicalProcessors

The result may look similar to this:

Caption                              NumberOfLogicalProcessors
-------                              -------------------------
Intel64 Family 6 Model 78 Stepping 3                         4 

When you run a process on such a machine, it typically has no specific processor affinity so Windows decides on which processor the process will run.

If you’d like, you can declare a specific processor affinity for each process. This can be useful, for example, if you’d like to control the processor(s) a program can use, i.e. to keep a process from using all processors.

Processor affinity is controlled by a bitflag. To find out the current processor affinity for a process, use the code below:

$process = Get-Process -Id $PID
[Convert]::ToString([int]$process.ProcessorAffinity, 2) 

In this example, the current PowerShell process is used, but you could specify any process. The typical result would be:


On a four processor machine, the process has no particular affinity and can use any processor. To set a new affinity, the easiest way is to use your own bit mask, and change the property. For example, to lock the current PowerShell process to the first processor only, try this:

# calculate the bit mask
$mask = '1000'
$bits = [Convert]::ToInt32($mask, 2)

# assign new affinity to current PowerShell process
$process = Get-Process -Id $PID
$process.ProcessorAffinity = $bits

Twitter This Tip! ReTweet this Tip!