Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function IsFileLocked($filePath){
- #write-host $filePath
- Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
- $errs.Count
- if ($errs.Count -ne 0)
- {
- return $true #File is locked
- }
- else
- {
- return $false #File is not locked
- }
- }
- $Path= "$env:temp"
- if ((Test-Path -Path $Path) -ieq $true)
- {
- $Daysback = '-30'
- $CurrentDate = Get-Date
- $DatetoDelete = $CurrentDate.AddDays($Daysback)
- get-childitem $Path -recurse | Where-Object {$_.LastWriteTime -lt $DatetoDelete } |
- Where-Object {$_.PSIsContainer -eq $False }| Where-Object {(IsFileLocked -filePath "($_)") -eq $false }# | remove-item -force #-WhatIf
- }
- $old = (Get-Date).AddDays(-30)
- Get-ChildItem $env:TEMP -Recurse |
- Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -lt $old } |
- Remove-Item -Force -ErrorAction SilentlyContinue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement