How to Cap a Specific Process to a Set of CPU Cores Using PowerShell
🔍 WiseChecker

How to Cap a Specific Process to a Set of CPU Cores Using PowerShell

You want to restrict a specific application or service to use only certain CPU cores on Windows 11 or Windows 10. This is often necessary when a background process monopolizes processor resources or when you need to ensure a critical application gets dedicated core access. Windows includes a built-in tool called the Processor Affinity mask that lets you bind a process to specific logical processors. This article explains how to use PowerShell to set CPU core affinity for any running process.

Key Takeaways: Setting Process Affinity with PowerShell

  • Get-Process and (Get-Process).ProcessorAffinity: Retrieve the current affinity mask of any running process.
  • Affinity mask calculation (binary to decimal): Convert the set of desired CPU cores into a decimal number that represents the core bitmask.
  • (Get-Process -Id PID).ProcessorAffinity = DecimalValue: Directly assign a new affinity mask to a process by its Process ID.

ADVERTISEMENT

What Is CPU Core Affinity and Why You Would Change It

Every running process on Windows has a property called Processor Affinity. This property defines which logical CPU cores the operating system scheduler is allowed to use when assigning threads from that process. By default, a process can run on any core. Changing the affinity restricts the process to a subset of cores.

You might want to cap a process to specific cores for several reasons. A resource-hungry application like a video encoder or a virtual machine host can be confined to a few cores so that other processes get the remaining cores. Some legacy software has licensing restrictions tied to the number of cores. In testing scenarios, you simulate a single-core or dual-core environment by limiting a process to one or two cores.

The affinity mask is a bitmask where each bit represents a logical processor. Core 0 is bit 0 value 1. Core 1 is bit 1 value 2. Core 2 is bit 2 value 4. Core 3 is bit 3 value 8, and so on. To assign cores 0 and 2, you add their values 1 + 4 = 5. The decimal value 5 is the affinity mask for cores 0 and 2.

Prerequisites

You need Windows PowerShell 5.1 or PowerShell 7 on Windows 10 or Windows 11. Run PowerShell as Administrator to change affinity for system processes. For your own user processes, Administrator rights are not required.

Steps to Cap a Specific Process to a Set of CPU Cores Using PowerShell

Follow these steps to identify the target process, calculate the correct affinity mask, and apply the change.

  1. Open PowerShell as Administrator
    Press the Windows key, type PowerShell, right-click Windows PowerShell or PowerShell, and select Run as administrator. Click Yes if prompted by User Account Control.
  2. Find the Process ID (PID) of the target process
    Run the command Get-Process -Name "notepad" | Format-Table Id, ProcessName replacing notepad with the actual process name. If you do not know the exact name, run Get-Process | Select-Object Id, ProcessName to list all running processes. Note the PID for the target process.
  3. Determine the decimal affinity mask for the desired cores
    Decide which CPU cores you want to assign. For example, to use cores 0 and 2 on a system with 4 logical cores, the binary mask is 0101 bits 0 and 2 set. The decimal value is 1 + 4 = 5. For easy calculation, use this PowerShell one-liner: $cores = @(0,2); $mask = 0; foreach ($c in $cores) { $mask += [math]::Pow(2, $c) }; $mask. This outputs the decimal mask value 5.
  4. Set the Processor Affinity for the target process
    Use the command (Get-Process -Id PID).ProcessorAffinity = DecimalMask. Replace PID with the actual process ID and DecimalMask with the decimal value from step 3. For example: (Get-Process -Id 1234).ProcessorAffinity = 5. This restricts the process to cores 0 and 2.
  5. Verify the change
    Run (Get-Process -Id PID).ProcessorAffinity to confirm the mask value matches your intended decimal number. The output shows the decimal mask. To see which cores are assigned, convert the decimal back to binary: [convert]::ToString((Get-Process -Id PID).ProcessorAffinity, 2). The binary string shows 1 for assigned cores.

Example: Capping Notepad to CPU Core 0 Only

Open Notepad. Run Get-Process -Name "notepad" | Format-Table Id, ProcessName and note the PID, for instance 5678. Calculate the mask for core 0 only: decimal value 1. Run (Get-Process -Id 5678).ProcessorAffinity = 1. Notepad now runs exclusively on core 0.

ADVERTISEMENT

Common Issues When Setting Process Affinity

“Access Denied” Error When Changing Affinity

If you see an Access Denied error, the process is running as a different user or is a protected system process. Close all instances of the process, then run PowerShell as Administrator. For system processes like svchost, you must run PowerShell as SYSTEM using tools like PsExec or the built-in Task Scheduler.

Affinity Resets After the Process Restarts

The affinity change is not permanent. When the process exits and restarts, Windows assigns the default affinity all cores. To make the change persist, create a scheduled task that runs the PowerShell command at startup or whenever the process launches. Use Task Scheduler to trigger the script on process start using Event Log events for process creation.

Incorrect Core Count or Mask Calculation

Windows numbers logical processors starting from 0. On a system with hyper-threading enabled, each physical core appears as two logical processors. Use Task Manager Performance tab to see the total number of logical CPUs. Double-check your binary-to-decimal conversion. A common mistake is using the number of cores as the mask value. For core 3 only, the mask is 8, not 3.

PowerShell Set-ProcessAffinity vs Manual Affinity Mask

Item PowerShell (Get-Process).ProcessorAffinity Task Manager Manual Affinity
Method Scriptable command-line assignment GUI checkboxes in Task Manager
Automation Fully automatable in scripts and scheduled tasks Must be done manually each time the process starts
Core selection Uses decimal mask derived from binary bitmask Check boxes for each logical processor
Required rights Administrator for system processes Administrator for all processes
Persistence Not persistent across restarts unless scripted Not persistent across restarts

You can now cap any running process to specific CPU cores using a single PowerShell command. This technique gives you fine-grained control over processor resource allocation without third-party tools. For permanent affinity, combine the command with a scheduled task that triggers on process creation. The core mask calculation follows a simple binary pattern that works identically on Windows 11 and Windows 10.

ADVERTISEMENT