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: (
[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: (
[Get-Item], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
and
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 }