Using Get-ChildItem to find archive folders; then recurse & delete if old

I have been working through a script that needs to parse about 2 TB of data, locate any “archive” folders, then look at the objects recursively inside those “archive” folders for old stuff. If any objects are found; log the absolute path, then delete.

I have a working script that can find all the “archive” folders, but am struggling with transitioning over to getting the child items under the archive folder.

This is what I have –

$TopLevelPaths = @(
"Local-shares"
, "Net-shares"
, "Net-Users"
, "Temp"
)

$DaysBack = "-400"
$CSVPath = "C:\Scripts\" 
$ErrorDataPath = "C:\Scripts\ErrorData.txt"
$CurrentDate = Get-Date
$DateToDelete = $CurrentDate.AddDays($DaysBack)
$MyFilter = "*archiv*"
$PathData = @()
$OldDataFile = $CSVPath + "OldData.csv"
$ArchiveLocations = @()

#// Find any flavor of "Archive" named folder and add to array
ForEach ($TopLevelPath in $TopLevelPaths) {
$CSVFile = $CSVPath + $TopLevelPath + "_Script_Output.csv"
$tempTopLevelPath = "D:\" + $TopLevelPath
$PathData += Get-ChildItem $tempTopLevelPath -Recurse -Force -ErrorAction SilentlyContinue -ErrorVariable ErrorData |
Where-Object {$_.PSIsContainer -and $_.Name -like $MyFilter} |
#Select-Object $_ | 
Select-Object @{l="Path";e={$_.PSPath.substring(38,$_.PSPath.Length-38)}}
$PathData | Export-CSV -NoTypeInformation -delimiter '|' $CSVFile
#// Works through Errors generated in Get-ChildItem command and drops them here
$ErrorData.Exception >> $ErrorDataPath
$ErrorData.TargetObject >> $ErrorDataPath
}

#// For Each entry in each CSV, Find Old Files
ForEach ($TopLevelPath in $TopLevelPaths) {
#// Build dynamic csv file names
$tempCSV = $CSVPath + $TopLevelPath + "_Script_Output.csv"
$ArchiveLocations = Import-CSV $tempCSV
ForEach ($ArchiveLocation in $ArchiveLocations) {
$OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force |
Select-Object FullName, LastWriteTime, Length |
Where-Object ($ArchiveLocation.LastWriteTime -lt $DateToDelete)
Write-Host OldFiles $OldFiles.Length
#$OldFiles | Export-CSV -NoTypeInformation -delimiter '|' $tempCSV
}
}

The error I’m getting back is

Get-ChildItem : Cannot find drive. A drive with the name '@{Path=D' does not exist.
At C:\Scripts\test.ps1:38 char:17
+ $OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{Path=D:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

OldFiles 0

Where Line 38 is

$OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force |

and the OldFiles 0 is just the Write-Host command at the bottom of the last ForEach loop. I’m thinking (or optimistic) that I am close, but not sure. Any help to make the leap from the first process to the next will be appreciated.

A sample of the output CSV file ($PathData) is below.

"Path"
"D:\Local-shares\Shares\Engineering\Archive"
"D:\Local-shares\Shares\Engineering\Archives"
"D:\Local-shares\Shares\Engineering\Archives\Manuals\archive"

As for…
I have been working through a script that needs to parse about 2 TB of data, locate any “archive” folders
… this will take a very long time, unless you break this up in chucks and leverage parallel and background job processing.

Looking at any store, that has multiple folder depth, requires the use of a recursive effort.

What do I have in my archive?

Finding out what is in the archive can be as simple as navigating to the folder and opening it in Windows Explorer (as I did yesterday). But I can also use the .NET Framework to do this. Unfortunately, the assembly to do this is not loaded by default (although if working with .zip files is a regular requirement, I would definitely add the assembly to the profile).
I load the assembly, then use a static method to find out what is in the .zip file:

Add-Type -AssemblyName “system.io.compression.filesystem”
[io.compression.zipfile]::OpenRead(“c:\fso\myarchive.zip”).entries.name
What doesn't work for verifying the contents of an archive? Get-ChildItem –Recurse Get-ChildItem –Force

The above is as per…

https://devblogs.microsoft.com/scripting/working-with-compressed-files-in-powershell-5

All that being said, using other methods, recursion has been shown as possible.

Example:

https://damashiuchi.wordpress.com/2011/02/19/get-child-items-in-a-zip-file-recursively-using-windows-powershell
function Get-ZipChildItems 
{param([string] $zZipFileName) 
      $shap = new-object -com shell.application 
      $zipFile = $shap.Namespace($zZipFileName) 
      $i = $zipFile.Items() 
      Get-ZipChildItems_Recurse $i 
}
function Get-ZipChildItems_Recurse 
{param([object]$items) 
      foreach($si in $items) 
      { 
           if($si.getfolder -ne $null) 
           { 
                #loop through subfolders 
                #------------------------- 
                Get-ZipChildItems_Recurse $si.getfolder.items() 
           } 
      #spit out the object 
      #--------------------- 
      $si 
      } 
} 
# main --------------------------------------------------------------- 
get-zipchilditems $args[0]