![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Retrieving LastWriteTime for files home drives I'm trying to determine when a home drive was last modified, so I'm retrieving the most recent LastWriteTime for a file in each home drive. I can pretty much achieve what I need with the script below, but I'm just being picky on the output. Basically I would like to output Home Drive, File, Last Write Time for each home drive: $homedirs = dir E:\users | where {$_.psIsContainer -eq $true} Foreach ($folder in $homedirs) { $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc | select -first 1 ` | select FullName, LastWriteTime $result } When I pipe the results to select I would also like to capture the home drive ($folder.FullName) e.g. something like " select $folder.FullName, FullName, LastWriteTime". However, I know this doesn't work. Any ideas? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Fenster wrote: Quote: > I'm trying to determine when a home drive was last modified, so I'm > retrieving the most recent LastWriteTime for a file in each home > drive. > > I can pretty much achieve what I need with the script below, but I'm > just being picky on the output. Basically I would like to output Home > Drive, File, Last Write Time for each home drive: > > $homedirs = dir E:\users | where {$_.psIsContainer -eq $true} > > Foreach ($folder in $homedirs) > { > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc > | select -first 1 ` > | select FullName, LastWriteTime > $result > } > > When I pipe the results to select I would also like to capture the > home drive ($folder.FullName) e.g. something like " select > $folder.FullName, FullName, LastWriteTime". However, I know this > doesn't work. > > Any ideas? (formatting might be off) $homedirs = dir e:\users | where {$_.psIsContainer -eq $true} Foreach ($folder in $homedirs) { $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc| select -first 1 ` | select @{n="folder";e={$folder.fullname}},FullName, LastWriteTime $result } ----------------- See for more examples: PSH>get-help select-object -examples Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Perfect! Thank you. I couldn't remember it was called a calculated property. On May 15, 10:45 am, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote: Quote: > Fenster wrote: Quote: > > I'm trying to determine when a home drive was last modified, so I'm > > retrieving the most recent LastWriteTime for a file in each home > > drive. Quote: > > I can pretty much achieve what I need with the script below, but I'm > > just being picky on the output. Basically I would like to output Home > > Drive, File, Last Write Time for each home drive: Quote: > > $homedirs = dir E:\users | where {$_.psIsContainer -eq $true} Quote: > > Foreach ($folder in $homedirs) > > { > > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc > > | select -first 1 ` > > | select FullName, LastWriteTime > > $result > > } Quote: > > When I pipe the results to select I would also like to capture the > > home drive ($folder.FullName) e.g. something like " select > > $folder.FullName, FullName, LastWriteTime". However, I know this > > doesn't work. Quote: > > Any ideas? > Sounds like all you need is to use calculated properties like this: > > (formatting might be off) > > $homedirs = dir e:\users | where {$_.psIsContainer -eq $true} > > Foreach ($folder in $homedirs) > { > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc| > select -first 1 ` > | select @{n="folder";e={$folder.fullname}},FullName, LastWriteTime > $result > } > ----------------- > > See for more examples: > PSH>get-help select-object -examples > > Marco > > -- > Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp > > PowerGadgets MVPhttp://www.powergadgets.com/mvp > > Blog:http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Thanks for this. Perfect. On May 15, 10:45 am, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote: Quote: > Fenster wrote: Quote: > > I'm trying to determine when a home drive was last modified, so I'm > > retrieving the most recent LastWriteTime for a file in each home > > drive. Quote: > > I can pretty much achieve what I need with the script below, but I'm > > just being picky on the output. Basically I would like to output Home > > Drive, File, Last Write Time for each home drive: Quote: > > $homedirs = dir E:\users | where {$_.psIsContainer -eq $true} Quote: > > Foreach ($folder in $homedirs) > > { > > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc > > | select -first 1 ` > > | select FullName, LastWriteTime > > $result > > } Quote: > > When I pipe the results to select I would also like to capture the > > home drive ($folder.FullName) e.g. something like " select > > $folder.FullName, FullName, LastWriteTime". However, I know this > > doesn't work. Quote: > > Any ideas? > Sounds like all you need is to use calculated properties like this: > > (formatting might be off) > > $homedirs = dir e:\users | where {$_.psIsContainer -eq $true} > > Foreach ($folder in $homedirs) > { > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc| > select -first 1 ` > | select @{n="folder";e={$folder.fullname}},FullName, LastWriteTime > $result > } > ----------------- > > See for more examples: > PSH>get-help select-object -examples > > Marco > > -- > Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp > > PowerGadgets MVPhttp://www.powergadgets.com/mvp > > Blog:http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives I would also like to do some date arithmetic to calculate the difference in days between the LastWriteTime and the current date. I tried this but couldn't seem to get it to work: $result = Get-Childitem ($folder.FullName) -Recurse | sort LastWriteTime -desc | Select-Object -first 1 ` | Select-Object @{Name="Home Drive";Expression={$folder.fullname}}, Name, ` LastWriteTime,@{Name="Age";Expression={(Get- Date).Subtract($result.LastWriteTime) | Select-Object Days}} And it returns values that look like this: Age --- @{Days=1} Thanks On May 15, 10:45 am, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote: Quote: > Fenster wrote: Quote: > > I'm trying to determine when a home drive was last modified, so I'm > > retrieving the most recent LastWriteTime for a file in each home > > drive. Quote: > > I can pretty much achieve what I need with the script below, but I'm > > just being picky on the output. Basically I would like to output Home > > Drive, File, Last Write Time for each home drive: Quote: > > $homedirs = dir E:\users | where {$_.psIsContainer -eq $true} Quote: > > Foreach ($folder in $homedirs) > > { > > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc > > | select -first 1 ` > > | select FullName, LastWriteTime > > $result > > } Quote: > > When I pipe the results to select I would also like to capture the > > home drive ($folder.FullName) e.g. something like " select > > $folder.FullName, FullName, LastWriteTime". However, I know this > > doesn't work. Quote: > > Any ideas? > Sounds like all you need is to use calculated properties like this: > > (formatting might be off) > > $homedirs = dir e:\users | where {$_.psIsContainer -eq $true} > > Foreach ($folder in $homedirs) > { > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc| > select -first 1 ` > | select @{n="folder";e={$folder.fullname}},FullName, LastWriteTime > $result > } > ----------------- > > See for more examples: > PSH>get-help select-object -examples > > Marco > > -- > Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp > > PowerGadgets MVPhttp://www.powergadgets.com/mvp > > Blog:http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Fenster wrote: Quote: > I would also like to do some date arithmetic to calculate the > difference in days between the LastWriteTime and the current date. $homedirs = dir c:\temp | where {$_.psIsContainer -eq $true} Foreach ($folder in $homedirs) { $result = dir ($folder.FullName) -Recurse | sort LastWriteTime -desc| select -first 1 ` | select @{n="folder";e={$folder.fullname}},FullName, LastWriteTime, @{n="days";e={($(get-date) - $_.lastwritetime).days}} $result } (Be careful with the formatting.) Marco |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Hopefully this is the last question ![]() I've been trying to write the output out to a csv file and have got this so far: $path = "C:\temp" $homedirs = Get-ChildItem $path | where {$_.psIsContainer -eq $true} $result = @() Foreach ($folder in $homedirs) { $result += Get-Childitem ($folder.FullName) -Recurse | Sort LastWriteTime -desc | Select -first 1 ` | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, ` LastAccessTime,@{Name="Age (Days)";Expression={($(Get-Date)- $_.LastAccessTime).Days}} $result[-1] } $result | Export-Csv c:\export.csv -noType -Force This works if there are only 3 sub-folders, if there are more than there I get this error: "Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null." Any ideas? On May 18, 6:26 pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote: Quote: > Fenster wrote: Quote: > > I would also like to do some date arithmetic to calculate the > > difference in days between the LastWriteTime and the current date. > How about something like this: > > $homedirs = dir c:\temp | where {$_.psIsContainer -eq $true} > > Foreach ($folder in $homedirs) > { > $result = dir ($folder.FullName) -Recurse | sort LastWriteTime > -desc| select -first 1 ` > | select @{n="folder";e={$folder.fullname}},FullName, > LastWriteTime, @{n="days";e={($(get-date) - $_.lastwritetime).days}} > $result > } > > (Be careful with the formatting.) > > Marco |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Fenster, You are adding to $result blindly without validating that get-childitem reproduce a fileinfo object. put this inside the foreach and look for False in the output: $result[-1] -eq $false or even color it : if($result[-1] -eq $false) { write-warning "current object is null" } Add a $null check before adding the object to $result: $obj + Get-Childitem ($folder.FullName) -Recurse | Sort LastWriteTime -desc | Select -first 1 ` | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, LastAccessTime,@{Name="Age (Days)";Expression={($(Get-Date)-$_.LastAccessTime).Days}} if($obj){$result+=$obj} --- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Hopefully this is the last question ![]() > > I've been trying to write the output out to a csv file and have got > this so far: > > $path = "C:\temp" > $homedirs = Get-ChildItem $path | where {$_.psIsContainer -eq $true} > $result = @() > Foreach ($folder in $homedirs) > { > $result += Get-Childitem ($folder.FullName) -Recurse | Sort > LastWriteTime -desc | Select -first 1 ` > | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, ` > LastAccessTime,@{Name="Age (Days)";Expression={($(Get-Date)- > $_.LastAccessTime).Days}} > $result[-1] > } > $result | Export-Csv c:\export.csv -noType -Force > This works if there are only 3 sub-folders, if there are more than > there I get this error: > > "Export-Csv : Cannot bind argument to parameter 'InputObject' because > it is null." > > Any ideas? > > On May 18, 6:26 pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> > wrote: > Quote: >> Fenster wrote: >> Quote: >>> I would also like to do some date arithmetic to calculate the >>> difference in days between the LastWriteTime and the current date. >>> >> >> $homedirs = dir c:\temp | where {$_.psIsContainer -eq $true} >> >> Foreach ($folder in $homedirs) >> { >> $result = dir ($folder.FullName) -Recurse | sort LastWriteTime >> -desc| select -first 1 ` >> | select @{n="folder";e={$folder.fullname}},FullName, >> LastWriteTime, @{n="days";e={($(get-date) - $_.lastwritetime).days}} >> $result >> } >> (Be careful with the formatting.) >> >> Marco >> |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives Thanks Shay, that pointed me in the right direction. I got the $null check working by using the following line: if($obj[-1]) {$result+=$obj[-1]} -Fenster On May 22, 1:27*pm, Shay Levi <n...@xxxxxx> wrote: Quote: > Fenster, > > You are adding to $result blindly without validating that get-childitem reproduce > a fileinfo object. > > put this inside the foreach and look for False in the output: > > $result[-1] -eq $false > > or even color it : > > if($result[-1] -eq $false) { > * *write-warning "current object is null" > > } > > Add a $null check before adding the object to $result: > > $obj + Get-Childitem ($folder.FullName) -Recurse | Sort *LastWriteTime -desc > | Select -first 1 ` > | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, LastAccessTime,@{Name="Age > (Days)";Expression={($(Get-Date)-$_.LastAccessTime).Days}} > > if($obj){$result+=$obj} > > --- > Shay Levi > $cript Fanatichttp://scriptolog.blogspot.com > > > Quote: > > Hopefully this is the last question ![]() Quote: > > I've been trying to write the output out to a csv file and have got > > this so far: Quote: > > $path = "C:\temp" > > $homedirs = Get-ChildItem $path | where {$_.psIsContainer -eq $true} > > $result = @() > > Foreach ($folder in $homedirs) > > { > > $result += Get-Childitem ($folder.FullName) -Recurse | Sort > > LastWriteTime -desc | Select -first 1 ` > > | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, ` > > LastAccessTime,@{Name="Age (Days)";Expression={($(Get-Date)- > > $_.LastAccessTime).Days}} > > $result[-1] > > } > > $result | Export-Csv c:\export.csv -noType -Force > > This works if there are only 3 sub-folders, if there are more than > > there I get this error: Quote: > > "Export-Csv : Cannot bind argument to parameter 'InputObject' because > > it is null." Quote: > > Any ideas? Quote: > > On May 18, 6:26 pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> > > wrote: Quote: Quote: > >> Fenster wrote: Quote: Quote: > >>> I would also like to do some date arithmetic to calculate the > >>> difference in days between the LastWriteTime and the current date. Quote: Quote: > >> How about something like this: Quote: Quote: > >> $homedirs = dir c:\temp | where {$_.psIsContainer -eq $true} Quote: Quote: > >> Foreach ($folder in $homedirs) > >> { > >> $result = dir ($folder.FullName) -Recurse | sort LastWriteTime > >> -desc| select -first 1 ` > >> | select @{n="folder";e={$folder.fullname}},FullName, > >> LastWriteTime, @{n="days";e={($(get-date) - $_.lastwritetime).days}} > >> $result > >> } > >> (Be careful with the formatting.) Quote: Quote: > >> Marco- Hide quoted text - > - Show quoted text - |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Retrieving LastWriteTime for files home drives I twicked the code a bit The select expressions can be merged into one,and to avoid $null objects down the pipline (and the if() statment), Get-ChildItem's results are piped to where. One thing to bear in mind: spaces in a column header names (e.g. calculated property), it can get you into *trouble* ![]() $path = "C:\temp" $homedirs = Get-ChildItem $path | where {$_.PSIsContainer} $HomeDrive = @{n="Home Drive";e={$_.fullname}} $Age = @{n="Age (Days)";e={((Get-Date)-$_.LastAccessTime).Days}} $homedirs | foreach { Get-ChildItem $_ -recurse | where {$_} | Sort LastWriteTime -desc | select $HomeDrive,Name,LastAccessTime,$age -first 1} | Export-Csv c:\export.csv -noType -force --- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Thanks Shay, that pointed me in the right direction. > > I got the $null check working by using the following line: > > if($obj[-1]) {$result+=$obj[-1]} > > -Fenster > > On May 22, 1:27 pm, Shay Levi <n...@xxxxxx> wrote: > Quote: >> Fenster, >> >> You are adding to $result blindly without validating that >> get-childitem reproduce a fileinfo object. >> >> put this inside the foreach and look for False in the output: >> >> $result[-1] -eq $false >> >> or even color it : >> >> if($result[-1] -eq $false) { >> write-warning "current object is null" >> } >> >> Add a $null check before adding the object to $result: >> >> $obj + Get-Childitem ($folder.FullName) -Recurse | Sort >> LastWriteTime -desc >> | Select -first 1 ` >> | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, >> LastAccessTime,@{Name="Age >> (Days)";Expression={($(Get-Date)-$_.LastAccessTime).Days}} >> if($obj){$result+=$obj} >> >> --- >> Shay Levi >> $cript Fanatichttp://scriptolog.blogspot.com Quote: >>> Hopefully this is the last question ![]() >>> >>> I've been trying to write the output out to a csv file and have got >>> this so far: >>> >>> $path = "C:\temp" >>> $homedirs = Get-ChildItem $path | where {$_.psIsContainer -eq $true} >>> $result = @() >>> Foreach ($folder in $homedirs) >>> { >>> $result += Get-Childitem ($folder.FullName) -Recurse | Sort >>> LastWriteTime -desc | Select -first 1 ` >>> | Select @{Name="Home Drive";Expression={$folder.fullname}},Name, ` >>> LastAccessTime,@{Name="Age (Days)";Expression={($(Get-Date)- >>> $_.LastAccessTime).Days}} >>> $result[-1] >>> } >>> $result | Export-Csv c:\export.csv -noType -Force >>> This works if there are only 3 sub-folders, if there are more than >>> there I get this error: >>> "Export-Csv : Cannot bind argument to parameter 'InputObject' >>> because it is null." >>> >>> Any ideas? >>> >>> On May 18, 6:26 pm, "Marco Shaw [MVP]" >>> <marco.shaw@_NO_SPAM_gmail.com> wrote: >>> >>>> Fenster wrote: >>>> >>>>> I would also like to do some date arithmetic to calculate the >>>>> difference in days between the LastWriteTime and the current date. >>>>> >>>> How about something like this: >>>> >>>> $homedirs = dir c:\temp | where {$_.psIsContainer -eq $true} >>>> >>>> Foreach ($folder in $homedirs) >>>> { >>>> $result = dir ($folder.FullName) -Recurse | sort LastWriteTime >>>> -desc| select -first 1 ` >>>> | select @{n="folder";e={$folder.fullname}},FullName, >>>> LastWriteTime, @{n="days";e={($(get-date) - >>>> $_.lastwritetime).days}} >>>> $result >>>> } >>>> (Be careful with the formatting.) >>>> Marco- Hide quoted text - >>>> >> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Retrieving files after a re-installation of Vista | Vista installation & setup | |||
| Retrieving files | Vista file management | |||
| Retrieving old files | Vista installation & setup | |||
| Retrieving deleted files | Vista file management | |||
| Visa encrypted old xp(home) files on the external drives? | Vista General | |||