Tags


 
We all know that I have a Deep love affair with Powershell. 
 
So much so my wife comes into the basement with a faint Blue haze over my face each night, wondering just WHAT the heck I’m doing huddled over my PC.
 
Today I got to play with Powershell again.   Sometimes it’s the simplest things it solves that makes it so wonderful.
 
How would YOU like to pull up a report on a file system of Folders which were modifed?  Easily?  and Free?
 
That is something you can do with Powershell without even trying.
 
In your Filesystem, the Modified Date field is easily accessible.   And easy to compare with any date.  Like say the current date?
 
So just simply assign the Date to a Variable with a GET-DATE
 
$CURRENTDATE=Get-Date
And of course pick a target to Scan
 
$TARGETDIRECTORY=’C:’
 
And pick how far to compare with, say anything modified in the last 60 days?
 
$COMPAREDATE=$CURRENDATE.Adddays(-60)
 
And then gather all those goodies into a variable to pick and choose with.
 
$FILELIST=GET-Childitem $TARGETDIRECTORY -recurse
 
And then casually dig through the list
 
Foreach ($file in $filelist)
{
     $Written=$FILE.LastWriteTime
     if ($written -gt $comparedate) { $FILE }
}
Now granted this is not the most beautiful code ever written but it’s easy to look at and modify.
 
We can take ALL of this and make it into a nice little script too prompting for the Number of days to look back as well as the folder to examine.
 
——————— LISTACCESSED.PS1 ——————————
 
$CURRENTDATE=Get-Date
 
# Ask for Folder to Scan
$TARGETDIRECTORY=READ-HOST ‘Which Directory to Scan?’
 
# Prompt for Number of days for output
$DAYSBACK=READ-HOST ‘How Many Days in the Past?’
 
$COMPAREDATE=$CURRENDATE.Adddays(-($DAYSBACK))
 
$FILELIST=GET-Childitem $TARGETDIRECTORY -recurse
 
Foreach ($FILE in $FILELIST)
{
     $Written=$FILE.LastWriteTime
     if ($WRITTEN -gt $COMPAREDATE) { $FILE }
}
——————— LISTACCESSED.PS1 ——————————
 
Now this is all nice a simple, dumping a list of information to your screen but run this SAME Script in Powershell and Pipe to an EXPORT-CSV and you’ll have information you can use in Excel or your choice of Spreadsheet and mine through your file system just to see what has been getting used (or not) for archival purposes.
 
LISTACCESSED.PS1 | EXPORT-CSV C:MYREPORT.CSV
 
Don’t you just love it when life get’s easy?
 
Sean
The Energized Tech
Advertisements