我对脚本比较陌生,下面是我用来清理一天前的临时文件的脚本,但我想构建一个不会删除活动(登录)用户的临时文件并通知哪些对象的检查被跳过。
#>
$oldTime = [int]1 # 1 day
# Create array containing all user profile folders
$colProfiles = Get-ChildItem "C:\Documents and Settings\" -Name
# Remove the "All Users" profile from the array
$colProfiles = $colProfiles -ne "All Users"
# Removes temporary files from each user profile folder
ForEach ( $objProfile in $colProfiles ) {
# Remove all files and folders in user's Temp folder
Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temp\*" -recurse | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | remove-item -force -recurse
# Remove all files and folders in user's Temporary Internet Files. The -force switch on Get-ChildItem gets hidden directories as well.
Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temporary Internet Files\*" -recurse -force | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | remove-item -force -recurse