0

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?

Eris
  • 7,378
  • 1
  • 30
  • 45
BigRedEO
  • 807
  • 4
  • 13
  • 33
  • 3
    Windows 7 is bundled with PowerShell v2. – user4003407 Jan 06 '16 at 18:15
  • 1
    I notice you're storing the output of gwmi in "StorePrinters.txt" and then select-string from CopyPrinters.txt with no step in between - is there code that was left out? – Sam Jan 06 '16 at 18:18
  • You load the entire file into memory with `Get-Content LexmarkSorted.txt` to count the lines and then reload all but the last line. Why not just load them into an array and then do something to lop off the last element from processing? E.g., `$lexmarkPrinters = Get-Content CopyLexmarkPrinters.txt | Sort-Object` then `$lexmarkPrinters | Select-Object -First ($lexmarkPrinters.Count - 1)`. Honestly, I'm not sure why you're even using all these files. It looks like this could all just be in memory arrays. – jpmc26 Jan 06 '16 at 18:25
  • Sam - I did leave out the line where I separate out this file. Edited above to correct – BigRedEO Jan 06 '16 at 18:43
  • PetSerAl - You are correct. Sorry. Version 2.0, but still no "Remote-Printer" as an option – BigRedEO Jan 06 '16 at 18:43
  • jpmc26 - The reason for all the file names is because there's actually three different printer names (all with Copy) and I need to delete all the copies, but keep the most recent of each of the three printer types, so they get split out into three separate text files (which I left out) – BigRedEO Jan 06 '16 at 18:44
  • I don't think that addresses what I suggested. You can split an array as easily as you can split a file, no? So the question is: why does this info need to be saved *on disk*? – jpmc26 Jan 06 '16 at 18:49
  • jpmc - for reference when logging onto a PC remotely. If something fails, I can view the text files to see where there might be an error along the way. (PS - a bit of a Powershell newbie, so bear with me) – BigRedEO Jan 06 '16 at 19:06

0 Answers0