Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. function IsFileLocked($filePath){
  2. #write-host $filePath
  3.  
  4. Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
  5. $errs.Count
  6. if ($errs.Count -ne 0)
  7. {
  8. return $true #File is locked
  9. }
  10. else
  11. {
  12. return $false #File is not locked
  13. }
  14. }
  15.  
  16. $Path= "$env:temp"
  17. if ((Test-Path -Path $Path) -ieq $true)
  18. {
  19. $Daysback = '-30'
  20. $CurrentDate = Get-Date
  21. $DatetoDelete = $CurrentDate.AddDays($Daysback)
  22.  
  23. get-childitem $Path -recurse | Where-Object {$_.LastWriteTime -lt $DatetoDelete } |
  24. Where-Object {$_.PSIsContainer -eq $False }| Where-Object {(IsFileLocked -filePath "($_)") -eq $false }# | remove-item -force #-WhatIf
  25. }
  26.  
  27. $old = (Get-Date).AddDays(-30)
  28.  
  29. Get-ChildItem $env:TEMP -Recurse |
  30. Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -lt $old } |
  31. Remove-Item -Force -ErrorAction SilentlyContinue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement