1: 新建文件 CpuNameChanger.ps1
bash
param(
[switch]$Startup,
[switch]$Restore
)
# ============================================================
# 配置
# ============================================================
# 修改成你希望显示的 CPU 名称
$CustomCpuName = "13th Gen Intel(R) Core(TM) i5-13400 CPU@3.20GHz"
# 开机任务名称
$TaskName = "Windows CPU Name Changer"
# 程序目录
$InstallDir = "C:\ProgramData\CpuNameChanger"
# 本脚本安装后的固定位置
$ScriptPath = "$InstallDir\CpuNameChanger.ps1"
# 原始 CPU 名称备份
$BackupFile = "$InstallDir\CpuNameBackup.txt"
# CPU 注册表
$CpuRegistryPath = "HKLM:\HARDWARE\DESCRIPTION\System\CentralProcessor"
# ============================================================
# 检查管理员权限
# ============================================================
function Test-Administrator {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
return $principal.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
}
# ============================================================
# 创建程序目录
# ============================================================
function Initialize-Directory {
if (!(Test-Path $InstallDir)) {
New-Item `
-Path $InstallDir `
-ItemType Directory `
-Force | Out-Null
Write-Host "已创建目录:" -ForegroundColor Green
Write-Host $InstallDir
}
}
# ============================================================
# 获取当前 CPU 名称
# ============================================================
function Get-CurrentCpuName {
$Path = "$CpuRegistryPath\0"
if (!(Test-Path $Path)) {
return $null
}
$Property = Get-ItemProperty `
-Path $Path `
-Name "ProcessorNameString" `
-ErrorAction SilentlyContinue
if ($null -ne $Property) {
return $Property.ProcessorNameString
}
return $null
}
# ============================================================
# 备份原始 CPU 名称
# ============================================================
function Backup-CpuName {
# 已经备份过就不再备份
if (Test-Path $BackupFile) {
Write-Host ""
Write-Host "检测到已有 CPU 名称备份,跳过备份。" `
-ForegroundColor Gray
return
}
$OriginalName = Get-CurrentCpuName
if ([string]::IsNullOrWhiteSpace($OriginalName)) {
Write-Host ""
Write-Host "无法获取原始 CPU 名称。" `
-ForegroundColor Red
return
}
# 确保目录存在
Initialize-Directory
$OriginalName |
Out-File `
-FilePath $BackupFile `
-Encoding UTF8 `
-Force
Write-Host ""
Write-Host "原始 CPU 名称已备份:" `
-ForegroundColor Yellow
Write-Host $OriginalName `
-ForegroundColor Yellow
}
# ============================================================
# 修改 CPU 名称
# ============================================================
function Set-CpuName {
if (!(Test-Path $CpuRegistryPath)) {
Write-Host ""
Write-Host "找不到 CPU 注册表路径:" `
-ForegroundColor Red
Write-Host $CpuRegistryPath
return
}
# 获取所有 CPU
$CpuKeys = Get-ChildItem $CpuRegistryPath
if ($null -eq $CpuKeys) {
Write-Host "没有找到 CPU 核心信息。" `
-ForegroundColor Red
return
}
foreach ($CpuKey in $CpuKeys) {
$Current = Get-ItemProperty `
-Path $CpuKey.PSPath `
-Name "ProcessorNameString" `
-ErrorAction SilentlyContinue
if ($null -eq $Current) {
continue
}
Set-ItemProperty `
-Path $CpuKey.PSPath `
-Name "ProcessorNameString" `
-Value $CustomCpuName `
-Force
Write-Host "CPU $($CpuKey.PSChildName) 修改成功:" `
-ForegroundColor Green
Write-Host " $CustomCpuName"
}
}
# ============================================================
# 创建开机自动执行任务
# ============================================================
function Install-StartupTask {
Initialize-Directory
# 当前脚本路径
$CurrentScript = $PSCommandPath
if ([string]::IsNullOrWhiteSpace($CurrentScript)) {
Write-Host ""
Write-Host "无法获取当前脚本路径。" `
-ForegroundColor Red
return
}
# 如果当前脚本不是安装目录中的脚本
if ($CurrentScript -ne $ScriptPath) {
Copy-Item `
-Path $CurrentScript `
-Destination $ScriptPath `
-Force
Write-Host ""
Write-Host "脚本已安装到:" `
-ForegroundColor Green
Write-Host $ScriptPath
}
# PowerShell 路径
$PowerShell = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
# 删除旧任务
schtasks.exe /Delete `
/TN "$TaskName" `
/F 2>$null | Out-Null
# 创建开机任务
schtasks.exe /Create `
/TN "$TaskName" `
/TR "`"$PowerShell`" -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$ScriptPath`" -Startup" `
/SC ONSTART `
/RU SYSTEM `
/RL HIGHEST `
/F | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "开机自动修改任务创建成功。" `
-ForegroundColor Green
Write-Host ""
Write-Host "任务名称:" -ForegroundColor Gray
Write-Host $TaskName
Write-Host ""
Write-Host "运行账户:SYSTEM" -ForegroundColor Gray
Write-Host "触发方式:Windows 开机" -ForegroundColor Gray
}
else {
Write-Host ""
Write-Host "开机任务创建失败。" `
-ForegroundColor Red
}
}
# ============================================================
# 删除开机任务
# ============================================================
function Remove-StartupTask {
schtasks.exe /Delete `
/TN "$TaskName" `
/F 2>$null | Out-Null
Write-Host ""
Write-Host "开机自动修改任务已删除。" `
-ForegroundColor Yellow
}
# ============================================================
# 恢复原始 CPU 名称
# ============================================================
function Restore-CpuName {
Write-Host ""
Write-Host "============================================"
Write-Host " 恢复原始 CPU 名称"
Write-Host "============================================"
if (!(Test-Path $BackupFile)) {
Write-Host ""
Write-Host "没有找到 CPU 名称备份文件:" `
-ForegroundColor Red
Write-Host $BackupFile
return
}
$OriginalName =
Get-Content $BackupFile -Encoding UTF8 |
Select-Object -First 1
if ([string]::IsNullOrWhiteSpace($OriginalName)) {
Write-Host "备份文件为空。" `
-ForegroundColor Red
return
}
Write-Host ""
Write-Host "准备恢复:" -ForegroundColor Yellow
Write-Host $OriginalName -ForegroundColor Green
$CpuKeys = Get-ChildItem $CpuRegistryPath
foreach ($CpuKey in $CpuKeys) {
$Current = Get-ItemProperty `
-Path $CpuKey.PSPath `
-Name "ProcessorNameString" `
-ErrorAction SilentlyContinue
if ($null -eq $Current) {
continue
}
Set-ItemProperty `
-Path $CpuKey.PSPath `
-Name "ProcessorNameString" `
-Value $OriginalName `
-Force
Write-Host "CPU $($CpuKey.PSChildName) 已恢复。" `
-ForegroundColor Green
}
# 删除开机任务
Remove-StartupTask
Write-Host ""
Write-Host "CPU 原始名称恢复完成。" `
-ForegroundColor Green
}
# ============================================================
# 开机启动模式
# ============================================================
if ($Startup) {
# 开机执行时不备份
# 直接设置名称
Set-CpuName
exit
}
# ============================================================
# 恢复模式
# ============================================================
if ($Restore) {
if (!(Test-Administrator)) {
Write-Host ""
Write-Host "恢复操作需要管理员权限。" `
-ForegroundColor Red
exit
}
Restore-CpuName
Write-Host ""
Write-Host "按 Enter 键退出..."
Read-Host
exit
}
# ============================================================
# 正常安装模式
# ============================================================
if (!(Test-Administrator)) {
Write-Host ""
Write-Host "============================================"
Write-Host "需要管理员权限运行。" `
-ForegroundColor Red
Write-Host "============================================"
Write-Host ""
Write-Host "请右键 PowerShell,选择:" `
-ForegroundColor Yellow
Write-Host "以管理员身份运行" `
-ForegroundColor Yellow
Write-Host ""
Read-Host "按 Enter 键退出"
exit
}
# ============================================================
# 开始安装
# ============================================================
Write-Host ""
Write-Host "============================================" `
-ForegroundColor Cyan
Write-Host " Windows CPU 名称修改工具" `
-ForegroundColor Cyan
Write-Host "============================================" `
-ForegroundColor Cyan
# ------------------------------------------------------------
# 创建目录
# ------------------------------------------------------------
Initialize-Directory
# ------------------------------------------------------------
# 显示原始 CPU
# ------------------------------------------------------------
$OriginalCpu = Get-CurrentCpuName
Write-Host ""
Write-Host "原来的 CPU:" `
-ForegroundColor Yellow
Write-Host $OriginalCpu
# ------------------------------------------------------------
# 显示目标 CPU
# ------------------------------------------------------------
Write-Host ""
Write-Host "修改为:" `
-ForegroundColor Yellow
Write-Host $CustomCpuName `
-ForegroundColor Green
# ------------------------------------------------------------
# 备份
# ------------------------------------------------------------
Write-Host ""
Write-Host "正在备份原始 CPU 名称..." `
-ForegroundColor Cyan
Backup-CpuName
# ------------------------------------------------------------
# 修改
# ------------------------------------------------------------
Write-Host ""
Write-Host "正在修改..." `
-ForegroundColor Cyan
Set-CpuName
# ------------------------------------------------------------
# 创建开机任务
# ------------------------------------------------------------
Write-Host ""
Write-Host "正在设置开机自动修改..." `
-ForegroundColor Cyan
Install-StartupTask
# ------------------------------------------------------------
# 最终结果
# ------------------------------------------------------------
Write-Host ""
Write-Host "============================================" `
-ForegroundColor Green
Write-Host " 修改完成" `
-ForegroundColor Green
Write-Host "============================================" `
-ForegroundColor Green
Write-Host ""
Write-Host "当前 CPU 显示名称:" `
-ForegroundColor Yellow
Write-Host (Get-CurrentCpuName) `
-ForegroundColor Green
Write-Host ""
Write-Host "开机后会自动重新设置 CPU 名称。" `
-ForegroundColor Green
Write-Host ""
Write-Host "原始名称备份:" `
-ForegroundColor Gray
Write-Host $BackupFile
Write-Host ""
Write-Host "如需恢复原始名称,请执行:" `
-ForegroundColor Yellow
Write-Host ""
Write-Host "powershell -ExecutionPolicy Bypass -File `"$ScriptPath`" -Restore" `
-ForegroundColor Cyan
Write-Host ""
Read-Host "按 Enter 键退出"
3: 执行一下命令
bash
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
Unblock-File -Path ".\CpuNameChanger.ps1"
.\CpuNameChanger.ps1