There are lots of examples showing the use of Get-Process myprocess
to change the processor affinity, and some examples using Get-WMIObject
; but these won't work for my use. I want to set processor affinity of my script as it executes. Is there a way to declare processor affinity when calling powershell.exe, or from within the script itself?
The exe I'm calling is in cygwin; which appears to change the affinity after being started with cmd.exe /c start <command> /affinity 16
This seems to work for me, but I haven't tested it. It seems to update what Task Manager sees, however.
$thisProcess = [System.Diagnostics.Process]::GetCurrentProcess();
$thisProcess.ProcessorAffinity = 0x1;
ProcessorAffinity
is a bitmask, so 0x1
is core 1, 0x2
is core 2, 0x4
is core 3, 0x8
is core 4, and so on. If you want to set cores 1-4, it's 0xF
, cores 1-9, 0x1FF
, etc. I don't think there's any magic value to set it back to "All".
You could also use Get-Process -Id $pid
, since $pid
is an automatic variable with the current process' PID.