From d3ade86eecf64a70f051412ab26e2fcb9e439f68 Mon Sep 17 00:00:00 2001 From: Daniel Dybing Date: Mon, 22 Dec 2025 16:39:40 +0100 Subject: [PATCH] fix: switch to System.Collections.ArrayList for robust collection handling --- PrintCleaner.ps1 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/PrintCleaner.ps1 b/PrintCleaner.ps1 index c70b1b4..cb07df7 100644 --- a/PrintCleaner.ps1 +++ b/PrintCleaner.ps1 @@ -70,8 +70,8 @@ function Get-InstalledSoftware { "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) - # Use Generic List to avoid "Hashtable can only be added to another" errors - $list = New-Object System.Collections.Generic.List[PSObject] + # Use ArrayList for maximum compatibility and to avoid fixed-size array errors + $list = New-Object System.Collections.ArrayList foreach ($path in $paths) { $items = Get-ItemProperty $path -ErrorAction SilentlyContinue @@ -85,7 +85,7 @@ function Get-InstalledSoftware { DisplayName = $name UninstallString = $cmd } - $list.Add($obj) + [void]$list.Add($obj) } } } @@ -148,8 +148,8 @@ function Invoke-RemovePrinters { Write-Host "`nStarting Cleanup..." -ForegroundColor Yellow - $removedPrinters = New-Object System.Collections.Generic.List[string] - $removedDrivers = New-Object System.Collections.Generic.List[string] + $removedPrinters = New-Object System.Collections.ArrayList + $removedDrivers = New-Object System.Collections.ArrayList # 1. Remove Printers try { @@ -164,7 +164,7 @@ function Invoke-RemovePrinters { Write-Host "[-] Removing Printer: $($p.Name)" try { Remove-Printer -Name $p.Name -ErrorAction Stop - $removedPrinters.Add($p.Name) + [void]$removedPrinters.Add($p.Name) } catch { Write-Host " [!] Failed to remove $($p.Name): $($_.Exception.Message)" -ForegroundColor Red } @@ -191,7 +191,7 @@ function Invoke-RemovePrinters { Write-Host "[-] Removing Driver: $($d.Name)" try { Remove-PrinterDriver -Name $d.Name -ErrorAction Stop - $removedDrivers.Add($d.Name) + [void]$removedDrivers.Add($d.Name) } catch { Write-Host " [!] Locked or in use (skipping): $($d.Name)" -ForegroundColor DarkGray } @@ -239,14 +239,14 @@ function Invoke-UninstallSoftware { $allSoftware = Get-InstalledSoftware - # Use Generic List to avoid type issues - $matches = New-Object System.Collections.Generic.List[PSObject] + # Use ArrayList to avoid type issues + $matches = New-Object System.Collections.ArrayList # Filter software list foreach ($sw in $allSoftware) { foreach ($brand in $brands) { if ($sw.DisplayName -match "(?i)\b$brand\b") { # Case insensitive regex match - $matches.Add($sw) + [void]$matches.Add($sw) break } }