powershell:检查在删除临时文件之前登录的用户

时间:2012-05-11 19:31:49

标签: powershell

我对脚本编程比较陌生,下面是我用来清理一天中临时文件的脚本,但是我想构建一个不会删除活动(登录)用户临时文件的检查通知哪些对象被跳过。

#>
$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

1 个答案:

答案 0 :(得分:0)

您可以在删除文件前测试用户名:

if ($objProfile -neq ( gwmi Win32_ComputerSystem ).username.split('\')[1]))
{ .. can delete ..}

if ($objProfile -neq [environment]::UserName)
    { .. can delete ..}