Jacob L
asked on
PowerShell, deleting files using last modified and last accessed
I had this question after viewing PowerShell, deleting files from multiple servers.
I had help with the below script to delete files from multiple servers. However, i now realize that some files are being used and need to delete files where the last acccessed time is less than x days and modified time is less than x days.
I had help with the below script to delete files from multiple servers. However, i now realize that some files are being used and need to delete files where the last acccessed time is less than x days and modified time is less than x days.
$User = "user"
$Password = Get-Content "D:\NCR\DAX\Scripts\psPW" | ConvertTo-SecureString
$Creds = New-Object System.Management.Automation.PsCredential -ArgumentList $user, $Password
$Servers = Get-Content D:\NCR\DAX\Scripts\servers.txt
# When you run an Invoke-Command command, Windows PowerShell runs the commands on all of the specified computers or in all of the specified PSSessions.This method will delete from all servers at the same.
Invoke-Command -ComputerName $Servers -Credential $Creds -ScriptBlock {
$date = (Get-Date).AddDays(-5)
Set-Location 'c:\Users\dax\AppData\Local\Temp'
Get-ChildItem -File | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force
}
Invoke-Command -ComputerName $Servers -Credential $Creds -ScriptBlock {
$date = (Get-Date).AddDays(-8)
Set-Location 'C:\Windows\Temp'
Get-ChildItem -File | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh. I just realized last accessed isn't the problem here. his file says last accessed in February. Not sure how its being used by another process, but i am thinking i need to exclude this file from the delete script
Looks like VMware logging. We can exclude open files or *.log or both.
ASKER
and would that be done like this
Invoke-Command -ComputerName $Servers -Credential $Creds -ScriptBlock {
$date = (Get-Date).AddDays(-5)
Set-Location 'c:\Users\dax\AppData\Loca l\Temp'
Get-ChildItem -File -exclude .log | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item -Force -WhatIf
}
How would you exclude open files?
Invoke-Command -ComputerName $Servers -Credential $Creds -ScriptBlock {
$date = (Get-Date).AddDays(-5)
Set-Location 'c:\Users\dax\AppData\Loca
Get-ChildItem -File -exclude .log | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item -Force -WhatIf
}
How would you exclude open files?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for late response, was still running into issues but i figured it out. This is what i needed. thank you.
ASKER
Cannot remove item C:\Windows\Temp\vmware-vms
+ CategoryInfo : WriteError: (C:\Windows\Temp\vmware-vm
+ FullyQualifiedErrorId : RemoveFileSystemItemIOErro