Guest User

Remove-UnlockedTempFiles.ps1

a guest
Nov 21st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. # Declare variable storing target user's username.
  2. $User = "TEST"
  3. # Set working directory to C:\Users\$User\AppData\Local\Temp.
  4. Set-Location -LiteralPath C:\Users\$User\AppData\Local\Temp
  5. # Declare variable storing a timestamp of 24 hours ago.
  6. $CurrentTimeMinus24Hours = (Get-Date).AddHours(-24)
  7. # Declare variable (array) storing list of files in temp directory.
  8. $Files = @(Get-ChildItem -Path . -File)
  9. # Declare variable storing Log file.
  10. $Log = "C:\TempFileDeletionLog.log"
  11.  
  12. # Function for getting a precise DateTime string for use in logging. It will be formatted for your timezone.
  13. function Get-DateTimeString {
  14. Get-Date -Format O
  15. }
  16.  
  17. <#
  18. Function to test whether or not a file is locked. Credit to Ben Baird here:
  19. https://social.technet.microsoft.com/Forums/windowsserver/en-US/74ea3752-9403-4296-ab98-d03fcc12b608/how-to-check-to-see-if-a-file-is-openlocked-before-trying-to-copy-it?forum=winserverpowershell
  20. #>
  21. function Test-FileLock
  22. {
  23. param
  24. (
  25. [parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
  26. [Alias("FullName")]
  27. [string]$Path,
  28. [switch]$PassThru
  29. )
  30. begin {}
  31. Process
  32. {
  33. $oFile = New-Object System.IO.FileInfo $Path
  34.  
  35. if ((Test-Path -Path $Path) -eq $false)
  36. {
  37. if ($PassThru.IsPresent -eq $false)
  38. {
  39. $false
  40. }
  41. return
  42. }
  43.  
  44. try
  45. {
  46. $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
  47. if ($oStream)
  48. {
  49. $oStream.Close()
  50. }
  51. if ($PassThru.IsPresent -eq $false)
  52. {
  53. $false
  54. }
  55. else
  56. {
  57. Get-Item $Path
  58. }
  59. }
  60. catch
  61. {
  62. # file is locked by a process.
  63. if ($PassThru.IsPresent -eq $false)
  64. {
  65. $true
  66. }
  67. }
  68. }
  69. end {}
  70. }
  71.  
  72. # ForEach loop that will test for multiple conditions, take actions based on those conditions, and log the results based on those actions for every file in the $Files array.
  73. $ErrorActionPreference = "SilentlyContinue"
  74. foreach ($File in $Files) {
  75. if ($File.LastWriteTime -lt $CurrentTimeMinus24Hours) {
  76. $File | Test-FileLock -PassThru | Remove-Item
  77. if ((Get-Item $File).Exists) {
  78. Add-Content -Path $Log -Value "$(Get-DateTimeString)> C:\$User\AppData\Local\Temp\$File was not deleted because it was locked."
  79. } else {
  80. Add-Content -Path $Log -Value "$(Get-DateTimeString)> C:\$User\AppData\Local\Temp\$File has been deleted."
  81. }
  82. } else {
  83. Add-Content -Path $Log -Value "$(Get-DateTimeString)> C:\$User\AppData\Local\Temp\$File was not deleted because it has been written to in the last 24 hours."
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment