deuceklub
deuceklub

Reputation: 21

Powershell to output folder based on content lastwritetime

I'm looking to create a powershell script that will look into the contents of a folder for the lasttimewrite on a file, or any file and output the folder and the time stamp. Using to idenrify profiles in a terminal server environment with raoming profiles, but folders lastwrite time is for the folder itself whereas each time a user logs in something inside is touched and timestamped.

I've attempt the following

Get-ChildItem -Recurse | where-object {$_.lastwritetime -gt (get-date).addDays(-1)} |  Foreach-Object { $_.Directory } | sort-object name -descending | export-csv c:\lists.csv

I've tried a couple iterations this seems to work

get-childitem -recurse | where-object {$_.name -eq "Pending"} |
where-object {$_.lastwritetime -gt (get-date).addDays(-1)} | 
where-object {$_.PSIsContainer} |
foreach {$_.Parent} | foreach {$_.name}

the only issue is now for the output putting the lastwritetime from the "Pending" folder displayed

Upvotes: 1

Views: 13678

Answers (2)

deuceklub
deuceklub

Reputation: 21

 $path = get-childitem | foreach {$_.name}
 $directory = $path | foreach {$_+"/"}
 get-childitem -path $directory | where-object {$_.name -eq "Pending"} |
 where-object {$_.lastwritetime -gt "12/30/2012"} | 
 where-object {$_.PSIsContainer} |
 foreach {$_.parent.name + "," + $_.lastwritetime} > 
 c:\users\opr9823\2013users.csv

this did it for me

Upvotes: 0

XXInvidiaXX
XXInvidiaXX

Reputation: 323

I am not completely sure, if I got your question right, but my little script shows all items, who have been changed in the last day and the time of change + the directory:

$directories = (Get-ChildItem -Path C:\test -Directory).FullName
     $directories | foreach  {if (($_.lastwritetime -gt (get-date).adddays(-1)) -or ((get-childitem -path $_ -Recurse).lastwritetime -gt (get-date).adddays(-1))) {
         $itemname = (Get-ChildItem -Path $_ -Recurse | where {$_.LastWriteTime -gt (get-date).AddDays(-1)}).BaseName
         $itemtime = (Get-ChildItem -Path $_ -Recurse | where {$_.LastWriteTime -gt (get-date).AddDays(-1)}).LastWriteTime
         $_ >> C:\timestamp.txt; $itemname >> C:\timestamp.txt; $itemtime >> C:\timestamp.txt;
                                                                                                                                                                  }
                             }

I got this back from my little script:

C:\test\DIR - Copy (2)
test (2)
test
test2 (2)
test2

Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43

I don't know how many files you have to check, so I can't say if this will be fast.

EDIT:

$path = C:\test
gci $PATH | sort LastWriteTime | select -last 1

Shows me this:

Directory: C:\test


Mode                LastWriteTime     Length Name                                                                                                                                    
 ----                -------------     ------ ----                                                                                                                                    
 d----        18.07.2013     09:37            Neuer Ordner - Kopie (2)       

Upvotes: 1

Related Questions