经常电脑用户名被设置成了 Administrator 甚至中文名, 如何完全改掉呢?

  1. 去到控制面板-用户账户修改
  2. 打开 cmd, 执行 whoami /user, 获取到该用户的 SID
  3. 打开注册表,进入到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
  4. 找到步骤2 中的 SID 目录, 修改 ProfileImagePath 为新的用户名
  5. 重启电脑, 此时会发现电脑会报错(恢复配置), 以及家目录下出现了一个 TEMP 的文件夹
  6. 第5步的问题是正常的, 此时直接将原本的用户名的家目录改为新的, 再次重启电脑就好了

这里写了一个 powershell 程序, 执行一下即可完成 2~3 步

# Get current user SID
$userSid = (whoami /user | Select-String -Pattern 'S-').ToString().Split()[1]

if (-not $userSid) {
    Write-Error "Failed to retrieve current user's SID."
    exit 1
}

Write-Output "Current user SID: $userSid"

$userInput = Read-Host -Prompt "Please confirm if the SID is correct. Type Y to continue, any other key to exit"

if ($userInput -ne 'Y' -and $userInput -ne 'y') {
    Write-Output "Operation cancelled."
    exit
}

# Prompt user to enter a new username
$newUserName = Read-Host -Prompt "Please enter the new user name"

# Output the entered user name
Write-Output "The new user name you entered is: $newUserName"

$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$userSid"

try {
    $profileImagePath = Get-ItemProperty -Path $regPath -Name "ProfileImagePath" -ErrorAction Stop
    Write-Output "Current ProfileImagePath: $($profileImagePath.ProfileImagePath)"
} catch {
    Write-Error "Failed to read registry path: $regPath"
    exit 1
}

$newProfilePath = "$newUserName"

try {
    Set-ItemProperty -Path $regPath -Name "ProfileImagePath" -Value $newProfilePath
    Write-Output "ProfileImagePath has been updated to: $newProfilePath"
} catch {
    Write-Error "Failed to modify ProfileImagePath"
    exit 1
}

如果执行遇到 因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies 的报错, 可以查看该解决方案解决方案