当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 Windows 中删除旧用户配置文件

作者:精品下载站 日期:2024-12-14 23:38:27 浏览:15 分类:玩电脑

如何在 Windows 中删除旧用户配置文件


管理员偶尔应从 Windows 工作站和服务器上的 C:\Users 中删除旧的用户配置文件(已退休或不活动的用户等)。 Windows 用户配置文件清理任务最常在远程桌面服务 (RDS) 终端服务器上执行。

RDS 服务器的主要问题是硬盘上用户配置文件目录大小的不断增长。此问题可通过使用 FSRM 或 NTFS 配额、使用漫游配置文件(例如 FSLogix 或用户配置文件磁盘)、重定向文件夹等的用户配置文件大小配额部分解决。但是,如果您有大量 RDS 用户,随着时间的推移,C:\Users 文件夹将包含大量包含旧(未使用)用户配置文件的目录。

如何手动删除Windows中的用户配置文件?

在 Windows 中,您可以从控制面板手动删除配置文件:

  1. 打开高级系统设置(运行命令

    SystemPropertiesAdvanced

    )并转到用户个人资料 -> 设置;

  2. 此窗口列出了存储在该计算机上的所有用户配置文件(本地、域和 Microsoft 帐户)。磁盘上每个用户配置文件的大小列在大小列中;

  3. 选择您要删除其个人资料的用户,然后单击删除按钮。

    [玩转系统] 如何在 Windows 中删除旧用户配置文件

在 Windows 11/10 和 Windows Server 2022/2019 上,您可以通过设置应用从磁盘删除用户配置文件。转到帐户 -> 访问工作和学校(或运行 URI 快捷方式

ms-settings:otherusers

)。选择一个用户并单击删除以从计算机中删除其个人资料数据。

[玩转系统] 如何在 Windows 中删除旧用户配置文件

在 Windows 中正确删除用户配置文件后,C:\Users 中的配置文件目录和注册表中的用户条目都会被删除。

许多新手管理员尝试从 C:\Users 文件夹中手动删除用户配置文件目录。在这种情况下,您需要从 Windows 注册表中手动删除配置文件引用:

  1. 运行注册表编辑器(

    regedit.exe

    );

  2. 转到注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

  3. 对于每个本地登录的用户(必须通过允许本地登录 GPO 选项允许该用户使用此登录方法),将创建一个单独的子项,并以用户的 SID 作为名称;

  4. 您可以通过 SID 找到与用户对应的注册表项,也可以手动浏览所有子项的内容,直到找到其中 ProfileImagePath 值指向磁盘上包含用户配置文件的目录的项(例如,

    C:\Users\j.smith

    );

    [玩转系统] 如何在 Windows 中删除旧用户配置文件

  5. 删除此注册表项即可完成配置文件的正确删除。

您还可以使用 PowerShell 删除特定用户的配置文件:

Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split(‘\’)[-1] -eq 'j.smith' } | Remove-CimInstance

此命令会删除硬盘驱动器目录和

j.smith

用户配置文件参考 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList 注册表。

此命令适用于 Windows PowerShell 和新版本的 PowerShell Core 6.x、7.x。

您可以使用 PowerShell Remoting 和 Invoke-Command cmdlet 删除远程计算机上的用户配置文件:

$compname="mun-wks92s3"
$user = "j.smith"
Invoke-Command -ComputerName $compname -ScriptBlock {
param($user)
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split(‘\’)[-1] -eq $user } | Remove-CimInstance
} -ArgumentList $user

GPO:删除早于指定天数的用户配置文件

在 Windows 中,有一个内置的组策略选项可以自动删除超过 xx 天的用户配置文件。您可以使用本地组策略编辑器启用此选项(

gpedit.msc

)或使用域 GPO 管理控制台(

gpmc.msc

)。在此示例中,我们将向 RDS 场中位于 Active Directory 中单独容器(组织单位,OU)中的主机应用自动配置文件清理策略。

在将清理配置文件策略应用到所有主机之前,我们强烈建议您在测试主机上对其进行测试。将其中一台 RDSH 服务器置于维护(耗尽)模式并在其上测试策略。

  1. 找到包含要应用用户配置文件清理策略的计算机/服务器的 OU。右键单击 OU,然后选择在此域中创建 GPO 并将其链接到此处

    [玩转系统] 如何在 Windows 中删除旧用户配置文件

  2. 指定策略名称并编辑GPO;

  3. 导航至计算机配置 -> 管理模板 -> 系统 -> 用户配置文件

  4. 打开选项在系统重新启动时删除早于指定天数的用户配置文件

  5. 启用策略并指定用户配置文件被视为活动的天数。此期限结束后,Windows User Profile Service 将在下次重新启动时自动删除该配置文件。建议在此指定45-90天的期限;

    [玩转系统] 如何在 Windows 中删除旧用户配置文件

  6. 应用新的组策略设置后,Windows Server 上的 User Profile Service 将自动删除旧的用户配置文件。用户配置文件将在下次服务器重新启动时删除。

如果您使用此策略,则必须确保服务器停止/重新启动时系统时间没有问题(请参阅文章“重新启动后系统时间和日期更改”)。否则,活动用户配置文件可能会被删除。

另一个缺点是您无法阻止某些配置文件被删除,例如本地帐户、管理员等。

此策略在 Windows 11/10 和 Windows Server 2022/2019 之前的版本中无法正常工作。以前,用户配置文件不活动是根据 NTUSER.dat 文件的修改日期来确定的。安装 Windows 更新时,受信任的安装程序服务可以更改每个用户配置文件中 NTUSER.dat 文件的修改日期。因此,Win32_UserProfile 服务认为该配置文件最近已被使用过。

在现代版本的 Windows 中,此组策略选项根据 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\ 下的 LocalProfileUnloadTimeLowLocalProfileUnloadTimeHigh 参数的值检查用户配置文件活动

[玩转系统] 如何在 Windows 中删除旧用户配置文件

您可以使用以下脚本获取正常时间格式的 LocalProfileLoadTimeLowLocalProfileUnloadTimeHigh 注册表值:

$profilelist = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
foreach ($p in $profilelist) {
    try {
        $objUser = (New-Object System.Security.Principal.SecurityIdentifier($p.PSChildName)).Translate([System.Security.Principal.NTAccount]).value
    } catch {
        $objUser = "[UNKNOWN]"
  }
    Remove-Variable -Force LTH,LTL,UTH,UTL -ErrorAction SilentlyContinue
    $LTH = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileLoadTimeHigh -ErrorAction SilentlyContinue).LocalProfileLoadTimeHigh
    $LTL = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileLoadTimeLow -ErrorAction SilentlyContinue).LocalProfileLoadTimeLow
    $UTH = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileUnloadTimeHigh -ErrorAction SilentlyContinue).LocalProfileUnloadTimeHigh
    $UTL = '{0:X8}' -f (Get-ItemProperty -Path $p.PSPath -Name LocalProfileUnloadTimeLow -ErrorAction SilentlyContinue).LocalProfileUnloadTimeLow
    $LoadTime = if ($LTH -and $LTL) {
        [datetime]::FromFileTime("0x$LTH$LTL")
    } else {
        $null
    }
    $UnloadTime = if ($UTH -and $UTL) {
        [datetime]::FromFileTime("0x$UTH$UTL")
    } else {
        $null
    }
    [pscustomobject][ordered]@{
        User = $objUser
        SID = $p.PSChildName
        Loadtime = $LoadTime
        UnloadTime = $UnloadTime
    }
} 

[玩转系统] 如何在 Windows 中删除旧用户配置文件

此列表包含每个用户配置文件的上次加载时间。

使用 PowerShell 脚本删除旧用户配置文件

您可以使用简单的 PowerShell 脚本来查找并删除已禁用或不活动用户的配置文件,而不是使用上述自动配置文件清理策略。

首先,我们尝试使用使用 PowerShell 获取文件夹大小一文中的简单脚本来计算 C:\Users 中每个用户配置文件的大小

gci -force ‘C:\Users\’-ErrorAction SilentlyContinue | Where { !($_.Attributes -match " ReparsePoint") }| ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$_.fullname, ‘{0:N2} GB’ -f ($len / 1Gb)
$sum = $sum + $len
}
"Total size of profiles",'{0:N2} GB' -f ($sum / 1Gb)

C:\Users 中所有用户配置文件的总大小约为 32 GB。

[玩转系统] 如何在 Windows 中删除旧用户配置文件

计算用户配置文件大小时,脚本会忽略 Windows 符号链接 (symlinks)。

让我们看看超过 60 天未使用个人资料的用户列表。您可以使用配置文件的 LastUseTime 字段中的值来查找它们。

Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-60))}| Measure-Object

事实证明,我的 RDS 主机上有 127 个不活动的用户帐户(配置文件总大小约为 18 GB)。

[玩转系统] 如何在 Windows 中删除旧用户配置文件

以下 PowerShell 脚本列出了超过 60 天未更新的用户配置文件的详细信息。该脚本将用户的 SID 转换为名称,计算每个用户配置文件的大小,并显示结果表:

$allprofilesinfo = @()
$OldProfiles=Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-60))}
Foreach ($OldProfile in $OldProfiles)
   {$objSID = New-Object System.Security.Principal.SecurityIdentifier ($OldProfile.SID)
    $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
        $userinfo = New-Object PSObject -Property @{
            userName = $objUser.Value
            ProfilePath = $OldProfile.localpath
            LastUsedDate = $OldProfile.ConvertToDateTime($OldProfile.LastUseTime)
            FolderSize =  "{0:N2} GB" -f ((gci -force $OldProfile.localpath -Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb) 
        }
    $allprofilesinfo += $userinfo
   }
$allprofilesinfo

[玩转系统] 如何在 Windows 中删除旧用户配置文件

要删除所有这些用户配置文件,只需将用户列表通过管道传送到 Remove-WmiObject 命令(建议您在运行之前使用 -WhatIf 参数检查脚本的输出):

Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-30))} | Remove-WmiObject -WhatIf

如前所述,在安装某些 Windows 更新时,受信任的安装程序服务可以更改每个用户配置文件中 NTUSER.dat 文件的修改日期。

上面的屏幕截图显示所有配置文件几乎同时更改。检查 Windows 中安装的最新更新的日期:

gwmi win32_quickfixengineering |sort installedon  |select InstalledOn -Last 1

或者使用 PSWindowsUpdate 模块:

Get-WUHistory | Select-Object -First 10

它很可能与配置文件更改的日期一致。因此,在早期版本的 Windows 上,您可以使用另一个检查用户配置文件目录的 lastwritetime 属性的脚本来获取非活动配置文件列表:

$USERS= (Get-ChildItem -directory -force 'C:\Users' | Where { ((Get-Date) — $_.lastwritetime).days -ge 60 } | % {'c:\users\' + $_.Name})
foreach ($User in $USERS) {
Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.LocalPath -eq $User)} | Remove-WmiObject WhatIf }

为了避免删除某些用户的配置文件(例如系统网络服务帐户、本地管理员帐户、具有活动会话的用户帐户以及例外列表中的其他帐户),您可以按如下方式修改脚本:

#The list of accounts, which profiles must not be deleted
$ExcludedUsers ="Public","zabbix_agent","svc",”user_1”,”user_2”
$LocalProfiles=Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-60))}
foreach ($LocalProfile in $LocalProfiles)
{
if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace("C:\Users\","")))
{
$LocalProfile | Remove-WmiObject
Write-host $LocalProfile.LocalPath, "profile deleted” -ForegroundColor Magenta
}
}

您可以在关机时通过 GPO 或使用任务计划程序中的 PowerShell 脚本运行此 PowerShell 脚本。

建议您在配置自动配置文件删除之前在您的环境中测试脚本!

您可以修改脚本以自动删除添加到特定 AD 组的所有用户配置文件。例如,您想要删除已退出的用户的个人资料。只需将这些帐户添加到 DisabledUsers 组并在目标主机上运行脚本即可:

$users = Get-ADGroupMember -Identity DisabledUsers | Foreach {$_.Sid.Value}
$profiles = Get-WmiObject Win32_UserProfile
$profiles | Where {$users -eq $_.Sid} | Foreach {$_.Delete()}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯