在系統重新啟動時,刪除超過所指定天數的使用者設定檔

 
但是實際執行時,會發現即便已超過設定時間,PROFILE仍未刪除。
原因是系統是依據C:\Users\$UserName\NTUser.dat的檔案時間做判斷,
因此需搭配powershell,
利用每日排程抓取$Path\$UserName\AppData\Local\Microsoft\Windows\UsrClass.dat的時間去取代NTUser.dat的時間
$ErrorActionPreference = “SilentlyContinue”
$Report = $Null
$Path = “C:\Users”
$ExcludedUsers = “Default”, “Public”, “Administrator”
$UserFolders = $Path | Get-ChildItem -Directory -Exclude $ExcludedUsers
 
ForEach ($UserFolder in $UserFolders)
{
    $UserName = $UserFolder.Name
    If (Test-Path “$Path\$UserName\NTUser.dat”)
    {
        $NTUserDat = Get-Item “$Path\$UserName\NTUSER.DAT” -force
        $NTUserDatTimeStamp = $NTUserDat.LastWriteTime
        $UsrClassDat = Get-Item “$Path\$Username\AppData\Local\Microsoft\Windows\UsrClass.dat” -force
        $UserClassTimeStamp = $UsrClassDat.LastWriteTime
        $NTUserDat.LastWriteTime = $UserClassTimeStamp
        Write-Host $UserName $NTUserDatTimeStamp
        Write-Host (Get-item $Path\$UserName\AppData\Local\Microsoft\Windows\UsrClass.dat -Force).LastWriteTime
        $Report = $Report + “$UserName`t$NTUserDatTimeStamp`r`n”
        $NTUserDat = $Null
        $UsrClassDat = $Null
    }
}