views:

866

answers:

2

I am trying to write a script that will output any directory that has not changed in over 90 days. I want the script to ONLY show the entire path name and lastwritetime. The script that I wrote only shows the path name but not the lastwritetime. Below is the script.

Get-ChildItem | Where {$_.mode -match "d"} | Get-Acl | 
    Format-Table @{Label="Path";Expression={Convert-Path $_.Path}},lastwritetime

When I run this script, I get the following output:

Path                                                        lastwritetime
----                                                        ----------
C:\69a0b021087f270e1f5c
C:\7ae3c67c5753d5a4599b1a
C:\cf
C:\compaq
C:\CPQSYSTEM
C:\Documents and Settings
C:\downloads

I discovered that the get-acl command does not have lastwritetime as a member. So how can I get the needed output for only the path and lastwritetime?

+3  A: 

You don't need to use Get-Acl and for perf use $_.PSIsContainer instead of using a regex match on the Mode property. Try this instead:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Format-Table FullName,LastWriteTime -auto

You may also want to use -Force to list hidden/system dirs. To output this data to a file, you have several options:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Select LastWriteTime,FullName | Export-Csv foo.txt

If you are not interested in CSV format try this:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Foreach { "{0,23} {1}" -f $_.LastWriteTime,$_.FullName} > foo.txt

Also try using Get-Member to see what properties are on files & dirs e.g.:

Get-ChildItem $Home | Get-Member

And to see all values do this:

Get-ChildItem $Home | Format-List * -force
Keith Hill
Keith Hill
Adding the -Recurse parameter to Get-ChildItem solve that particular problem.
Keith Hill
Yes I figured that out about the -recurse option after I posted. But one more question and I will be out of your hair.Even the format-table has the auto option, when you out put the data to a file the lastwritetime column gets dropped. Also, using the wrap option makes the output look kinda of messy.I believe the format-table is automatically taking the column width of the screen and putting it into the file.Is it possible to created custom column widths using the ft command?
omegared_xmen
I updated the answer to show a better way to write the info to file.
Keith Hill
That worked. Thanks very much for your help.
omegared_xmen
A: 

Thanks.

But I have another question. Is it possible that a child directory can change but not the parent? If so, does this script check for that as well?

omegred_xmen
Please add your comments to the associated answer rather than posting a new "answer". :-)
Keith Hill