Windows - VsCode导致Windows凭据过多之一键删除

Windows - VsCode导致Windows凭据过多之一键删除

起因

都2026年了,电脑上Github CLI gh版本还是2023年的gh version 2.32.1 (2023-07-24),不支持多账号登录等一些特性,更换新版本gh version 2.87.0 (2026-02-18)后运行报错:

bash 复制代码
failed to migrate config: cowardly refusing to continue with multi account migration: couldn't migrate oauth token for "github.com": Not enough memory resources are available to process this command.
# 或者报错:
failed to move active token in keyring: Not enough memory resources are available to process this command.

我64G的内存跑不了一个gh身份token的迁移?(bushi),一搜发现Github Desktop(#15217)有类似的问题,评论说删个旧凭据可能有效。

一看凭据,发现有大量的2022-2023年间VsCode存储的微软账号登录凭据,指定是当时设计的有点问题。

是时候删除多余凭据了。

脚本批量删除

粗略估计这样的凭据至少有100个,手动删除肯定不是程序员的作风,于是(几轮对话)让豆包写了个脚本:
script

powershell 复制代码
# 强制要求管理员权限
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

# 匹配包含 vscodevscode 的 LegacyGeneric 类型凭据
$matchKeyword = "vscodevscode"

try {
    Write-Host "=== 正在读取所有Windows凭据 ===" -ForegroundColor Cyan
    $allCreds = cmdkey /list
    $targetCreds = @()

    # 精准解析格式:目标: LegacyGeneric:target=xxx
    foreach ($line in $allCreds) {
        # 匹配包含 LegacyGeneric:target= 且有 vscodevscode 的行
        if ($line -match "目标:\s*LegacyGeneric:target=(.*$matchKeyword.*)") {
            $credFullName = $matches[1].Trim()
            # 拼接完整的删除参数(必须带 LegacyGeneric:target= 前缀)
            $fullTarget = "LegacyGeneric:target=$credFullName"
            $targetCreds += $fullTarget
        }
    }

    # 处理匹配结果
    if ($targetCreds.Count -eq 0) {
        Write-Host "`n❌ 未找到包含 '$matchKeyword' 的凭据" -ForegroundColor Red
        Read-Host "按回车退出"
        exit
    }

    Write-Host "`n✅ 找到 $($targetCreds.Count) 个VSCode相关凭据:" -ForegroundColor Green
    $targetCreds | ForEach-Object { Write-Host "  - $_" }

    # 确认删除
    $confirm = Read-Host "`n是否确认删除这些凭据?(Y/N)"
    if ($confirm -notmatch "^[Yy]$") {
        Write-Host "`n🚫 用户取消删除" -ForegroundColor Yellow
        Read-Host "按回车退出"
        exit
    }

    # 执行删除(关键:必须用完整的 LegacyGeneric:target=xxx 作为删除参数)
    $deleted = 0
    foreach ($cred in $targetCreds) {
        try {
            # 用cmd调用避免PowerShell转义问题
            $output = cmd /c "cmdkey /delete:`"$cred`"" 2>&1
            if ($LASTEXITCODE -eq 0) {
                Write-Host "✅ 已删除:$cred" -ForegroundColor Green
                $deleted++
            } else {
                Write-Host "❌ 删除失败:$cred (错误:$output)" -ForegroundColor Red
            }
        }
        catch {
            Write-Host "❌ 删除异常:$cred (错误:$($_.Exception.Message))" -ForegroundColor Red
        }
    }

    Write-Host "`n📊 删除完成!成功删除 $deleted / $($targetCreds.Count) 个凭据" -ForegroundColor Green
}
catch {
    Write-Host "`n❌ 脚本执行出错:$($_.Exception.Message)" -ForegroundColor Red
}

Read-Host "按回车退出"

使用记事本 另存为xx.ps1,编码为ANSI,管理员身份启动powershell,输入这个脚本的路径并回车,脚本会先列举出以vscodevscode.开头的凭据,按Y回车可以批量删除,世界开始变得清净了。

End

删了当年留下的182个凭据后,gh表现正常了。
The Real End, Thanks! 同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

相关推荐
xx~t9 小时前
嵌入式——Linux软件编程——网络1
linux·c语言·网络·vscode·网络协议
jyOverQ9 小时前
ChatGPT Windows 客户端点击无反应、有进程却没窗口?一次 MSIX 非系统盘启动卡死的完整排查
windows·chatgpt
sunburn-11 小时前
Java堆(Heap)详解与实战教学
java·开发语言·数据结构·ide·算法
还是大剑师兰特12 小时前
MySQL8.0 Windows完整安装教程
windows·mysql·大剑师
redfred14 小时前
HandBrake 使用教程:开源视频压缩工具 视频太大压缩 MKV转MP4 批量转码 硬件加速 Windows/macOS/Linux
windows·开源·音视频
逐步前行14 小时前
ESP32-S3-VSCode扩展ESP-IDF
ide·vscode·编辑器
kakakahahahaha15 小时前
【Windows】C盘拒绝访问0x80070005的7步排查
windows·电脑·内容运营·软件需求
171320330计算机毕设编程16 小时前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
万年咸鱼17 小时前
Java ArrayList 详解:原理、用法与实战
java·开发语言·windows
奈斯先生Vector17 小时前
本地图片识别怎么接入多模态 AI?用 Python API 理解 GPT-4o Vision 的真实工作流
开发语言·人工智能·windows·python·网络协议·http·aigc