#1 23 Jul 2010 14:54

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,011
Website

Finding unused/dormant storage folders

Here is a script to print the newest file in each of a series of top level folders.

This is similar to what the 'diruse' or du utilities do but looking at age rather than filesize, so is useful for finding dormant folders that are not being used/written to.

# List all top level folders (not files)
dir "\\fileserver01\Workgroups" | ?{$_.PsIsContainer -eq $true} |
foreach {
   # For each folder look through all the files (not folders) and display the most recent file.
   dir $_.fullname -recurse | ?{$_.PsIsContainer -ne $true}| sort -prop LastWriteTime | select -last 1
 }

The script above is not particularly fast as it will touch every file in the hierarchy, and has to do some fairly big sorts.

Offline

#2 20 Nov 2010 08:51

jumper
Member
From: tunisia, Mornaguia
Registered: 17 Apr 2009
Posts: 55
Website

Re: Finding unused/dormant storage folders

Simon Sheppard wrote:

The script above is not particularly fast as it will touch every file in the hierarchy, and has to do some fairly big sorts.

PS  ~walid2mi> 
                  (measure-command {
                  dir . | ?{$_.PsIsContainer -eq $true} |
                  foreach {
                   # For each folder look through all the files (not folders) and display the most recent file.
                  dir $_.fullname -recurse | ?{$_.PsIsContainer -ne $true}| sort -prop LastWriteTime |select -last 1}
                  }).Milliseconds

>>> 131
PS  ~walid2mi> 
                  (measure-command {
                  foreach($item in (dir . | ? {$_.PSIsContainer})) {
                  dir $item.fullname -rec | ?{!$_.PsIsContainer}| sort LastWriteTime | select -last 1}
                 }).Milliseconds

>>> 126 
PS  ~walid2mi> 
Simon Sheppard wrote:

| sort -prop LastWriteTime | select -last 1

another test:

PS  ~walid2mi> 
                (measure-command {ps|sort id|select id}).MilliSeconds

>>> 15

PS  ~walid2mi> 
                (measure-command {ps|select id|sort id}).MilliSeconds

>>> 11

Offline

#3 20 Nov 2010 13:28

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,011
Website

Re: Finding unused/dormant storage folders

Interesting, I can see how doing the select before a sort would speed things up, but I'm surprised the 'foreach($item' in your first example is that much faster than a pipe.

Offline

#4 10 Jan 2011 01:44

saintly
Member
Registered: 10 Jan 2011
Posts: 1

Re: Finding unused/dormant storage folders

Hi,
i'm a Powershell novice so forgive me if this is a basic question
I have worked out how to get the results of the code above to output into a file like this
foreach($item in (dir . | ? {$_.PSIsContainer})) {dir $item.fullname -rec | ?{!$_.PsIsContainer}| sort LastWriteTime | select -last 1 >>c:\temp\output.txt}
and it seems to work fine.
Is it possible for me to now modify the script to only list the files/folders that the latest file is older than a specified date or number of days.
What i'm looking to do is to list any folders that have not had a file accessed in X number of days or since XX/XX/XXXX date (which ever is easier)

Thanks
Ian

Offline

#5 04 Feb 2011 13:55

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,011
Website

Re: Finding unused/dormant storage folders

Take a look at this page
http://ss64.com/ps/syntax-delolder.html

You can just adapt that to list the files instead of deleting them

Offline

Board footer

Powered by FluxBB