0

Based on this question at StackOverflow, I'm able to change processor affinity if an executable is running only in 1 instance with this command:

PowerShell "$Process = Get-Process java; $Process.ProcessorAffinity=11"

If 2 or more instances are running, I cannot change, and this is the output

C:\PowerShell "$Process = Get-Process java; $Process.ProcessorAffinity=11"
The property 'ProcessorAffinity' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:30
+ $Process = Get-Process java; $Process.ProcessorAffinity=11
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Does anyone knows how to change Processor Affinity for all java.exe instances using Powershell?

2
  • 1
    what happens if you change your command to the following? PowerShell "$Process = Get-Process java | % { $_.ProcessorAffinity=11 }"
    – SimonS
    Dec 8, 2016 at 15:18
  • 2
    This is an elegant solution and should be posted as an answer. The variable can also be removed, Get-Process java | % { $_.ProcessorAffinity=15 }.
    – root
    Dec 8, 2016 at 15:27

1 Answer 1

5

You have to loop over each object to set its ProcessorAffinity

| % {} in PowerShell means ForEach-Object and is basically the same as a foreach() statement in other languages

as root said, you can remove the variable so your code gets shorter.

from a cmd window:

PowerShell "get-process java | % { $_.ProcessorAffinity=11 }"

in a batch file (the batch file handles % like a variable, so you need to write it 2 times or switch to foreach) :

PowerShell "get-process java | %% { $_.ProcessorAffinity=11 }"
PowerShell "get-process java | foreach { $_.ProcessorAffinity=11 }"

directly in PowerShell:

get-process java | % { $_.ProcessorAffinity=11 }
5
  • Totally successful! Worked like a charm. And thank you for the explanation about | % {}, that's what I needed.
    – adamitj
    Dec 8, 2016 at 22:29
  • Just a correction: When calling from inside a batch file the percent sign must be doubled: PowerShell "get-process java | %% { $_.ProcessorAffinity=11 }"
    – adamitj
    Dec 8, 2016 at 23:08
  • @adamitj are you sure? that wouldn't make any sense at all, since you would double the Foreach-Object. If I run that with %% I get an error. with % it works.
    – SimonS
    Dec 9, 2016 at 8:03
  • Yes, for sure. A single percent sign inside a batch file is intended to reference an argument passed outside (calling from OS or from cmd.exe) as in %1, %2, or referencing another environment variable like %tmp%, etc. If I want to print one percent sign with echo, for example, I need to echo %%
    – adamitj
    Dec 9, 2016 at 9:22
  • @adamitj you're right! the bat-file doesn'0t behave the same as the cmd window itself. I will update my answer
    – SimonS
    Dec 9, 2016 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .