I have about 300 off-site PCs (Windows 7 PCs, Powershell version 1.0 - so "Remove-Printer" is not an option) which have a number of "Copy" printers (thanks to Microsoft's infinite helpfulness in making a "Copy" every time my users change ports) I need to delete. I will never know how many "Copy" printers there are, so I've come up with this code so far to delete all but the most recent printer (no matter how many "Copy" printers there are) -
#GET LIST OF PRINTERS
Get-WMIObject -Class Win32_Printer | Select Name > StorePrinters.txt
#GET ALL "Lexmark" PRINTERS
Select-String StorePrinters.txt -Pattern "Lexmark" | ForEach-Object {$_.Line} > CopyPrinters.txt
#GET ALL "Lexmark Universal v2 PS3" PRINTERS
Select-String CopyPrinters.txt -Pattern "PS3" | ForEach-Object {$_.Line} > CopyLexmarkPrinters.txt
#SORT THE Lexmark Universal v2 PS3" PRINTERS
Get-Content CopyLexmarkPrinters.txt | Sort-Object > LexmarkSorted.txt
#GET NUMBER OF Lexmark PRINTERS, THEN DELETE ALL BUT THE MOST RECENT COPY
$LEXlines = Get-Content LexmarkSorted.txt | Measure-Object -line | Select-Object -expand Lines
Get-Content LexmarkSorted.txt -totalcount ($LEXlines - 1) | ForEach-Object
{
cd C:\Windows\System32\Printing_Admin_Scripts\en-US
Write-Host "cscript prnmngr.vbs -d -p `"$_`""
cmd /c "cscript prnmngr.vbs -d -p `"$_`""
cd \Support
}
When I try to run it, it pops up a box with
"cmdlet ForEach-object at command pipeline position 2 - Supply values..." (gets cut off)
"Process[0]"
and is expecting input. What am I doing wrong here?