PowerShell删除Windows用户配置文件脚本时间戳异常问题求助
PowerShell删除Windows用户配置文件脚本时间戳异常问题求助
各位大佬好,我最近碰到个头疼的问题,想请大家帮忙出出主意:
我们的多用户设备磁盘总是被占满,所以我写了个PowerShell脚本用来清理旧的用户配置文件,但现在发现一个关键问题——不知道是Windows本身还是某个后台进程,一直在修改配置文件的LastUseTime属性,导致所有用户配置文件的时间戳都一模一样,根本没法依据这个判断哪些是长期未使用的旧配置文件。
还有两个前提情况需要说明:
- 我没办法通过GPO来做这个清理,因为我们小组没有GPO的管理权限
- 之前试过用DelProf2工具,结果它也遇到了同样的时间戳异常问题
下面是我目前写的脚本:
# 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..."
想问问各位大佬,有没有办法解决这个时间戳不准的问题?或者有没有其他更可靠的方式来判断用户配置文件的最后实际使用时间呢?
备注:内容来源于stack exchange,提问作者Ed Palmer
相关产品推荐
相关产品推荐

