Solved

PowerShell, deleting files using last modified and last accessed

Posted on 2016-11-11
7
30 Views
Last Modified: 2016-11-16
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.

$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
}

Open in new window

0
Comment
Question by:jacobJL
  • 4
  • 2
7 Comments
 
LVL 14

Accepted Solution

by:
Jason Crawford earned 250 total points
ID: 41883945
If I'm understanding the requirements correctly, you should be able to just modify line 11 to this:

Get-ChildItem -File | Where-Object {$_.LastWriteTime -lt $date -and $_.LastAccessTime -lt $date} | Remove-Item -Force

Open in new window

0
 

Author Comment

by:jacobJL
ID: 41884059
SMH. That's exactly what i had but i forgot hyphen in front of the and operator. However after corrected i still get this error

Cannot remove item C:\Windows\Temp\vmware-vmsvc.log: The process cannot access the file 'C:\Windows\Temp\vmware-vmsvc.log' because it is being used by another process.
    + CategoryInfo          : WriteError: (C:\Windows\Temp\vmware-vmsvc.log:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
0
 

Author Comment

by:jacobJL
ID: 41884063
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41884085
Looks like VMware logging.  We can exclude open files or *.log or both.
0
 

Author Comment

by:jacobJL
ID: 41884113
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\Local\Temp'
  Get-ChildItem -File  -exclude .log | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item -Force -WhatIf
}

How would you exclude open files?
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 41886104
You'll need to use -exclude *.log - it needs to be a file mask, no substring match.
The brute force approach to get over open files is to just ignore the error message. Add -ea SilentlyContinue to the remove-item.
0
 

Author Closing Comment

by:jacobJL
ID: 41890028
Sorry for late response, was still running into issues but i figured it out. This is what i needed. thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now