PoshCode Archive  Artifact Content

Artifact 31565f609637d41402094bad378a319cb18f7fb87eecf6304b1bae1a34f28289:

  • File Search-Files-by-Date.ps1 — part of check-in [33ebfc84aa] at 2018-06-10 13:31:23 on branch trunk — Please: If you download and like, please tweet it. (user: Matt Schmitt size: 2248)

# encoding: ascii
# api: powershell
# title: Search Files by Date
# description: Please:  If you download and like, please tweet it.
# version: 1.0
# type: script
# author: Matt Schmitt
# license: CC0
# x-poshcode-id: 3806
# x-archived: 2012-12-04T12:10:48
# x-published: 2012-11-30T09:15:00
#
# This project is in response to “Need PS script to search files by date accessed” script request: http://gallery.technet.microsoft.com/scriptcenter/site/requests/Need-PS-script-to-search-files-by-date-accessed-66042eaf?ShowSubmitLinkForm=False
# This is a script that will export a csv file containing the names of files for files in a certain directory (including sub-directories) within a date range.  When ran, the script asks for the root directory, the from and to dates, and where to export the file to.
# # Enjoy!
# Matt
#
<#
  Author:   Matt Schmitt
  Date:     11/30/12 
  Version:  1.0 
  From:     USA 
  Email:    ithink2020@gmail.com 
  Website:  http://about.me/schmittmatt
  Twitter:  @MatthewASchmitt
  
  Description
  A script for finding files in a directory by Last Accessed Date.  
#>


Write-Host "Enter Root Directory you would like to search"
Write-Host ""
Write-Host "Example: C:\Users\testuser"
Write-Host ""
$directory = Read-Host ">>"

cls
Write-Host "Enter "From" Date"
Write-Host ""
Write-Host "Example: 1/1/12"
Write-Host ""
$startDate = Read-Host '>>'

cls
Write-Host "Enter "To" Date"
Write-Host ""
Write-Host "Example: 11/30/12"
Write-Host ""
$endDate = Read-Host '>>'

cls
Write-Host "Enter where you would like the CSV file to be exported"
Write-Host ""
Write-Host "Example: C:\Files.csv"
Write-Host ""
$outPutFile = Read-Host '>>'

cls
Write-Host ""
Write-Host ""

Write-Host "Searching for files in '$directory' from '$startDate' to '$endDate'..."

Get-ChildItem -path $directory -Recurse  | where { $_.lastaccesstime -ge [datetime]$startDate -and $_.lastaccesstime -lt [datetime]$endDate} | select fullname | Export-CSV -Path $outPutFile

Write-Host ""
Write-Host "Results have been exported!"
Write-Host ""
Write-Host "Press any key to Exit."

#Code to wait for user to press a key.
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")