Friday, June 6, 2014

#14 : Find all the files modified within last 7 days

Such requirements come to picture many times in different forms. Below is piece of code which can be used for such requirements. I have made it recursive to all subdirectories also.

  1. clear   
  2.   
  3. #--Change the name with directory you want to visit --#    
  4. $dir_to_look="C:\Users\admin\Desktop"    
  5.   
  6. #--You may change the number of days of your choice --#   
  7. $seven_days_backdate=$(Get-Date).AddDays(-7)    
  8.   
  9. #--Find the files which are modified or created within last 7 days --#    
  10. Get-Childitem $dir_to_look -Recurse | `   
  11.         where-object {!($_.psiscontainer)} | `   
  12.         where { $_.LastWriteTime -gt $seven_days_backdate } | `   
  13.         foreach {  Write-Host "$($_.LastWriteTime) :: $($_.Fullname) "  }   

2 comments:

  1. Hi som, any way to export results to CSV?

    ReplyDelete
  2. To export to csv modify the last line to the below.

    foreach {Write-Output "$($_.LastWriteTime) :: $($_.Fullname) " >> C:\scripts\dir2.csv }

    don't forget to change the C:\scripts\dir2.csv to the location on your machine where you would like the csv file exported.

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...