Last modified File starting 2 folders deep, cycling through all subfolders

How do I combine these two scripts to pull the info from the first one (latest modified file with date/time), but start two levels down and loop through all subfolders like in the second one. When I insert the first one into the second at “Get-Item”, it errors out.

Get-ChildItem -Recurse  | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize 
$StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
$Depth = 15      # How many levels deep to scan
$Path = "."     # starting path

For ($i=$StartLevel; $i -le $Depth; $i++) {
    $Levels = "\*" * $i
    (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
    Select FullName
}

try this

 

$StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
$Depth = 15 # How many levels deep to scan
$Path = "." # starting path

$folders = For ($i=$StartLevel; $i -le $Depth; $i++) {
$Levels = "\*" * $i
(Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
Select FullName
}

$Folders.fullname | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }

WOW! I think that’s it!!

It runs well, except I am getting this error and I think it’s happening when there aren’t subfolders to 15 maybe?

Get-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At line:7 char:44

  • (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsConta …
  • CategoryInfo : InvalidData: (:slight_smile: [Get-Item], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
    and

I’m thrilled with what you have done so far!!

How do I fix the Null error?

How do I output the results to a file?

 

And how do I mark your answer as awesome?

I got it to output to file. How do I fix the error when the subfolder doesn’t exist down to the 15 level?

Get-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At line:7 char:44

  • (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsConta …
  • CategoryInfo : InvalidData: (:slight_smile: [Get-Item], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
    and

or suppress it

I think if you just check if it is something like this

$Folders.fullname | ?{$_} | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }

Or maybe if you don’t want to see them just ignore them.

$Folders.fullname | %{ Get-ChildItem -path $_ -Recurse -erroraction silentlycontinue | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize }