Skip to content

Instantly share code, notes, and snippets.

@maxwellb
Last active March 16, 2026 07:17
Show Gist options
  • Select an option

  • Save maxwellb/672b5b0620429083544b24503d3bb871 to your computer and use it in GitHub Desktop.

Select an option

Save maxwellb/672b5b0620429083544b24503d3bb871 to your computer and use it in GitHub Desktop.
Pwsh snippets
function Remove-CpuAffinity
{
$whoami = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
$radmin = [Security.Principal.WindowsBuiltInRole]::Administrator
if (-not $whoami.IsInRole($radmin)) {
Write-Error "This function requires Administrator privileges to write to HKLM."
return
}
$init = $Host.UI.Prompt(
'Set CPU affinity by aversion',
@'
Specify the application name and affinity 'aversions'
app_name:
program name ending in .exe
avert:
comma-separated list of CPU numbers to unmask
'@
,
@('app_name', 'avert')
)
if (-not ($init.app_name -ilike '*.exe'))
{
$init.app_name = '{0}.exe' -f $init.app_name
}
$init.avert = [int[]]$init.avert.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries)
$app = $init.app_name
$avert = $init.avert
$amask = (-bnot 0ul) ; $avert |% { $amask = $amask -band (-bnot(1ul -shl $_)) } ;
if (
0 -eq
$Host.UI.PromptForChoice(
'Confirmation required',
@"
Set CPU Affinity mask for ${app} to $( ('{0:B}' -f $amask).PadLeft(64, '0') -replace '(....)', '$1 ' ) ?
"@
,
('&Yes', '&No'),
1
)
)
{
$k = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\{0}' -f $app
$null = New-Item -Path $k -ErrorAction SilentlyContinue
$null = New-ItemProperty -Path $k -Name AffinityMask -PropertyType QWORD -Value $amask -Force
Get-Item -Path $k
}
else
{
$null
}
# end function Remove-CpuAffinity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment