注意:我无法通过 GPO 执行此操作,因为我的团队对此没有控制权。DelProf2 在时间戳方面也存在同样的问题。
由于多用户设备上的驱动器已满,我编写了一个脚本来删除旧配置文件。我认为问题在于 Windows 或其他进程正在修改 LastUseTime,以便所有配置文件都具有相同的标记。
# Prompt the user for the number of days
$days = Read-Host "Enter the number of days to keep profiles"
# Define the list of users to protect
$protected_users = @("administrator","all users","default","default user","public")
# Get a list of all user profiles
$profiles = Get-ChildItem -Path "C:\Users" -Directory
# Loop through each profile and delete if it's older than the specified number of days
foreach ($profile in $profiles) {
$username = $profile.Name
$last_write = $profile.LastUseTime
$age = New-TimeSpan -Start $last_write -End (Get-Date)
# Skip the profile if it belongs to a protected user
if ($protected_users -contains $username) {
Write-Host "Skipping protected user profile: $username"
continue
}
# Delete the profile if it's older than the specified number of days
if ($age.TotalDays -ge $days) {
Write-Host "Deleting user profile: $username"
Remove-Item -Path $profile.FullName -Recurse -Force
}
}
# Exit and delete the script
Read-Host "Press any key to exit..."