omegared_xmen
omegared_xmen

Reputation: 21

Powershell, trying to output only the path and lastwritetime on directories

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?

Upvotes: 2

Views: 8107

Answers (1)

Keith Hill
Keith Hill

Reputation: 201622

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

Upvotes: 6

Related Questions