In our previous post, we looked at time stamping offline printers. Today, we are going to track and delete offline printers. The end result will be a cleaned up print server! Oh yeah – all of this is done with PowerShell!
Starting off, we need to configure three options: our printserver, logging location, and past offline date.
#Configuration Block #PrintServer to Connect to $PrintServer = "YOUR SERVER" #Set Location for Logging Set-Location "C:\Users\Public\Logs" #Get Past Date $Date= (Get-Date).AddMonths(-1)
Right now, this script only looks at printers that have been offline for one month. You can adjust this back by decreasing the (-1) value. For example, to see printers that have been offline for three months, change this line to:
$Date= (Get-Date).AddMonths(-3)
Next, we need to get our list of offline printers and to filter out any that haven’t been offline for longer than our $Data variable.
#Create List of Offline Printers $OfflinePrinters = get-printer -ComputerName $PrintServer -name ADM*,BHS*,GA*,GICA*,PBX*,RAX* | Where-Object { $_.Comment -lt $Date -and $_.Comment -ne $null} | Sort-Object Name #Display Offline Printers get-printer -ComputerName $PrintServer -name ADM*,BHS*,GA*,GICA*,PBX*,RAX* | Where-Object { $_.Comment -lt $Date -and $_.Comment -ne ""} | Select-Object Name,PrinterStatus,PortName,Comment | Sort-Object Name | Format-Table -AutoSize
While the block above may look duplicated, I like to also see an exported list of offline printers. You will also notice that I am filtering the printer names. If you would like to see all printers, replace the -name ADM* … with just -name *. Now that we have our list, we need to delete the printers and clean up the unused printer port.
#Delete Offline Printers ForEach-Object { Remove-Printer -Name $OfflinePrinters.Name -ComputerName $PrintServer -Confirm Remove-PrinterPort -Name $OfflinePrinters.PortName -ComputerName $PrintServer -Confirm }
In the block above, we use the ForEach-Object cmdlet to cycle through every offline printer. Each Remove line has a -confirm parameter for added safety. Finally, I like to have a second way of documenting steps like this. Here is how we can document this:
#Document Deletion $Date= Get-Date $TimeStamp = "These printers were deleted on $Date." $TimeStamp | Out-file DeletedPrinters.CSV -Append $OfflinePrinters| Out-file DeletedPrinters.CSV -Append
And that is all there is to it! For reference, here is the complete script. Please let me know if you have any questions!
#Configuration Block #PrintServer to Connect to $PrintServer = "YOURPRINTSERVER" #Set Location for Logging Set-Location "Log file location #Get Past Date $Date= (Get-Date).AddMonths(-1) #Create List of Offline Printers $OfflinePrinters = get-printer -ComputerName $PrintServer -name ADM*,BHS*,GA*,GICA*,PBX*,RAX* | Where-Object { $_.Comment -lt $Date -and $_.Comment -ne $null} | Sort-Object Name #Display Offline Printers get-printer -ComputerName $PrintServer -name ADM*,BHS*,GA*,GICA*,PBX*,RAX* | Where-Object { $_.Comment -lt $Date -and $_.Comment -ne ""} | Select-Object Name,PrinterStatus,PortName,Comment | Sort-Object Name | Format-Table -AutoSize #Delete Offline Printers ForEach-Object { Remove-Printer -Name $OfflinePrinters.Name -ComputerName $PrintServer -Confirm Remove-PrinterPort -Name $OfflinePrinters.PortName -ComputerName $PrintServer -Confirm } #Document Deletion $Date= Get-Date $TimeStamp = "These printers were deleted on $Date." $TimeStamp | Out-file DeletedPrinters.CSV -Append $OfflinePrinters| Out-file DeletedPrinters.CSV -Append
This is awesome! Thank you! Just one tiny thing I noticed when you copied and pasted, you forgot the terminating quotation mark on line 6.
Hi, How if I only want to delete the local offline printer device, not the network printer? thank you