Compare printers on printer servers

Right now I´m working on removing two old printer servers that should have been powered of a couple of years ago. Unfortunately we still have some GPOs mapping the printers to some users with VB scripts. Therefore I need to find out if we have printers on the old servers that haven’t been migrated to the new servers, to accomplish that I collected all printers from the old and new printer servers in different variables and then used compare-object on the printers portname. On the new printer servers we have chosen to place all Toshiba-printers on one server and all Oki and HP on another one, therefore I could easily separate Toshiba printers from the others with an if else  statement filtering on the driver name. The result is a list of all printers that don’t exist on the new servers displaying which printer server the printer exist on and which printer server it needs to be installed on:

“The printer PRN0422 exists on srv-print04 and needs to be installed on srv-print08”

Now the printer/GPO-guy can install the printers on the correct servers and replace the Vb scripts with preferences mapping the same printers but on the new servers.


#Get all printers
$srvprint04 = Get-printer -ComputerName srv-print04
$srvprint05 = Get-Printer -ComputerName srv-print05
$srvprint07 = Get-Printer -ComputerName srv-print07
$srvprint08 = Get-Printer -ComputerName srv-print08

#Collect old and new printers in diffrent variables
$printers1 = $srvprint04 + $srvprint05
$printers2 = $srvprint07 + $srvprint08

#Compare if printers exists on new server
$allold= Compare-Object -ReferenceObject $printers2.portname -DifferenceObject $printers1.portname -IncludeEqual | where sideindicator -EQ "=>"

#Display which old server the printer is installed on and which new server it needs to be installed on.
foreach ($printer in $printers1)
{ if ($allold.inputobject -contains $printer.portname   )
  {if ($printer.drivername -like "*tosh*")
    {Write-Host The printer $printer.PortName exists on $printer.ComputerName and needs to be installed on srv-print07
      {Write-Host The printer $printer.PortName exists on $printer.ComputerName and needs to be installed on srv-print08}

Bulk delete printers


Right now I´m working on project to dispose two old print servers.  A issue with these servers is that no one used a routine to delete printers from the server when they physically powered off the printers mapped on the server. To solve this I used powershell to collect all printers from the server and then do a ping to the portname of each printer and list the ones who didn’t respond, in an array. Then I use that array to delete the printers from the server.

#NAME: Delete_Printers.ps1
#AUTHOR: Viktor Lindström
#COMMENTS: Delete printers on remote server that don´t respond on ping.

#Collect all printers
$AllPrinters = Get-Printer -ComputerName srv-print05 | where portname -Like "*prn*"

#create an empty array
$DownPrinters = @()

#collect printers that don't respond on ping 
foreach ($printer in $AllPrinters)
    $DownPrinters += Test-NetConnection $printer.PortName -ErrorAction SilentlyContinue | where pingsucceeded -eq $false
    Write-Host "testar ping mot" $printer.PortName

# Delete printers that didn´t respond on ping
foreach ($Printer2 in $DownPrinters)
$Getprint = Get-Printer -ComputerName srv-print05 | where portname -EQ $printer2.computername 
Remove-Printer -InputObject $Getprint

Delete expired accounts in Active Directory



In an facebook user group another user asked how to use powershell to delete expired user accounts. He wanted to use excel to store expiration date and username etc etc. Since AD have accountexpirationdate property built in, it is better to use the AD as content database instead of excel. I answers with this simple script and since the reply got the thumbs up i think it solved his problem.

$days_date = Get-Date
$users = get-aduser -Filter 'AccountExpirationDate -LT $days_date'

foreach ($usr in $users)
{Remove-ADUser $usr

This is just a simple core to remove users, you could expand the script to for example use a quarantine lets say you want to use a quarantine for 60 days, I would create a quarantine OU and instead of deleting users that has expired “today” i would move them to that OU. After that you could create a new  get-date string but with the add_days method you could remove 60 days so you only remove user accounts older then 60 days:

$date = (get-date).AddDays(-60)

You could also build a log. To do that you could begin with an empty hash table in front of the foreach-loop and then use the name property from the $usr and $get-date do get the date when it was removed, after that you have to use the GetEnumerator method when you export to CSV, it could look something like this:

$date = get-date
$log = @{}
foreach ($usr in $users)
$log.GetEnumerator() | select name, value | export-csv -NoTypeInformation C:\test\log.csv