You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量清理学生电脑C:\Users目录下用户配置文件并排除指定文件夹的批处理方案求助

Correct Batch Script to Clean User Profiles (Excluding Specific Folders)

First, let's break down why your original script didn't work as expected:

  • A typo: goto Ende instead of goto End meant the Default folder wasn't properly excluded.
  • No loop to iterate through all folders in C:\Users—your script only processed a single folder passed as %1, not every profile directory.
  • Lack of case-insensitive checks, so folders with mismatched capitalization would get deleted accidentally.

Here's a robust batch script that meets your requirements: it excludes your specified folders, deletes the rest, and cleans up corresponding registry entries safely:

@echo off
setlocal enabledelayedexpansion

:: Define your list of excluded user folders (quoted names handle spaces)
set "excluded_folders=Administrator "All Users" Default Default.lic "Default User" defaultuser0 Public 40040 40041"

:: Loop through every directory in C:\Users
for /d %%G in ("C:\Users\*") do (
    set "current_folder=%%~nG"
    set "is_excluded=0"

    :: Check if the current folder is in our exclusion list (case-insensitive)
    for %%E in (%excluded_folders%) do (
        if /i "!current_folder!"=="%%~E" (
            set "is_excluded=1"
            goto :skip_deletion
        )
    )

    :: Delete the user profile folder (silently and recursively)
    echo Deleting user profile: !current_folder!
    rmdir /S /Q "%%G"

    :: Clean up corresponding registry entries in ProfileList (targets only this user's SID)
    powershell -Command "Get-Item 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-*' | Where-Object { ($_.GetValue('ProfileImagePath') -split '\\')[-1] -eq '!current_folder!' } | Remove-Item -Recurse -Force"

    :skip_deletion
)

endlocal
echo Profile cleanup finished successfully.

Key Features:

  • Case-insensitive checks: Ensures folders like administrator (lowercase) are still excluded.
  • Space-friendly handling: Quoted entries in excluded_folders work seamlessly with names like "All Users" or "Default User".
  • Safe registry cleanup: Instead of using a broad wildcard, it targets only the SID entries linked to the deleted profile, avoiding accidental system registry modifications.
  • Transparent feedback: Echoes which profiles are being deleted so you can verify the process in real time.

Important Notes:

  • Run this script as Administrator—deleting user profiles and modifying the registry requires elevated privileges.
  • Test first: Replace rmdir /S /Q "%%G" with echo Would delete: "%%G" to preview which folders will be removed before executing the actual deletion.

内容的提问来源于stack exchange,提问作者IT-Support PHF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:02:35