Powershell script that would remove printers from print servers base on CSV file and add new printer objects to print servers


I am looking for two  Powershell scripts
First PS script should remove printers objects from print servers " c:\temp\servers.txt"
Second PS Script should add new printers objects to printer servers.

1. PS script should  pull data from CSV file "Corppr2.csv",  this file will contain all information about printers that  need to remove or add.
2. PS script should  content -path from c:\temp\servers.txt
3. PS script should run against any print servers from that "servers.txt" list and execute script to remove or add printers
4. Output file" Log file" should be generated

File "corppr2.csv" is the  source of printer objects  that would need to be attached to Powershell script for execution

Please help, Thank you Mike
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

michalek19Author Commented:
OK, let me try to put some staff together
File server name should be pull from the  txt file ($Printservers = Get-Content  ( server.txt))
CSV file should be pull from  $InputFile =  CSV file

This is CSV file and its format
Printserver         Driver          Portname      IPAddress Sharename      Location Commen  Printername  Bidirectional  BannerSheet
xxxxx Xerox WorkCentre 7435 PCL6      TRUE       Only 7435 Room3  BlackWihte  Only 7435  Disable  Disable
xxxxx Xerox WorkCentre 7435 PCL7      TRUE  7436 Room4  ColorPrinter Test  7436 Disable Disable

Here is part of the script to add printers objects that works but is missing function for BiDirectional and Bannersheet
$DebugPreference = “silentlyContinue”

$printers = Import-Csv c:\temp2\printers.csv

function CreatePrinter
$server = $args[0]
$print = ([WMICLASS]”\\$server\ROOT\cimv2:Win32_Printer”).createInstance()
$print.drivername = $args[1]
$print.PortName = $args[2]
$print.Shared = $true
$print.Sharename = $args[3]
$print.Location = $args[4]
$print.Comment = $args[5]
$print.DeviceID = $args[6]
$print.PrintProcessor = $args[7]
$print.Rawonly = $true
$print.DoCompleteFirst = $true

function CreatePrinterPort
$server = $args[0]
$port = ([WMICLASS]”\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort”).createInstance()
$port.Name= $args[1]
$port.HostAddress= $args[2]

foreach ($printer in $printers)
CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername $printer.PrintProcessor

I don't have script to remove printobjects

Please let me know if this is to many information or if is not clear

Thx, Mike
Bob LearnedCommented:
Since that uses the Win32_Printer class from WMI, enabling bidirectional is in the EnableBidi property:

Win32 Printer Class

I have no idea about BannerSheet, though.
michalek19Author Commented:
I need help to modify printers removal script to pull printers from CSV  and pull  print server names from '.txt" file.
CSV file should have

This is CSV file and its format
Printserver         Driver          Portname      IPAddress Sharename      Location Commen  Printername  Bidirectional  
xxxxx            Xerox WorkCentre 7435 PCL6      TRUE       Only 7435 Room3  BlackWihte  Only 7435  Disable  

$PrintersTodelete = Get-WmiObject -Class Win32_printer -ComputerName $server -filter "name='printer0456'"
     Foreach($printer in $PrintersTodelete){
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Bob LearnedCommented:
You have a lot going on in this question, so it was confusing what your main question is...

Did you figure out the solution to your own question?
michalek19Author Commented:
i have a script to disable bidirectional.

I am still mot getting any luck with printer objects removal.  

Do you have any example of the script that would pull printers from csv file and remove them on print server?
Bob LearnedCommented:
"I am still not getting any luck with printer objects removal."
What issues are you having?

I don't have any example script, but I am willing to help you get over your issues.
michalek19Author Commented:
So, here is a script. When i ran this script nothing happen to printer object

$Source = "C:\temp2\rmprinters.csv"
#-----Create object to use data in the csv file-----#
$printers = Import-Csv $Source
#$printers = Import-Csv c:\temp2\

function CreatePrinterPort     {      
$port = ([WMICLASS]"\\LocalHost\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()      
$port.Name = $args[1]      
$port.SNMPEnabled = $false      
$port.Protocol = 1
$port.HostAddress = $args[2]    }
#-----create function block to RECREATE the PRINTER object-----#
function CreatePrinter     {      
$print = ([WMICLASS]"\\LocalHost\ROOT\cimv2:Win32_Printer").createInstance()        
$print.drivername = $args[1]      
$print.PortName = $args[2]      
$print.Shared = $true      
$print.Sharename = $args[3]      
$print.Location = $args[4]      
$print.Comment = $args[5]      
$print.DeviceID = $args[6]      
$print.Rawonly = $true      
$print.DoCompleteFirst = $true    }
#-----Foreach loop to Call the function blocks and declare which properties, that are outlined in the CSV, will be used by the script.-----#
foreach ($printer in $printers)     {      
<#DeletePrinter $printer.Printserver $printer.Printername        DeletePrinterPort $printer.Printserver $printer.Portname#>
$printer.Printername     }
Bob LearnedCommented:
1) Does $printers have > 0 printers

2) What Powershell version are you using?

3) I see that Powershell 4.0 has the Remove-Printer cmdlet for Windows Server 2012 R2 and Windows 8.1:


4) Have you seen this?

Hey, Scripting Guy! How Can I Use Windows PowerShell to Remove Old Printer Connections, List Printers, and Set New Default Printers?

Function Remove-Printer($printObject, [string]$printerPath)
 Write-Host -foregroundcolor cyan "Removing $printerPath"
  $dp = $printObject |
 Where-Object { $_.deviceID -match $printerPath }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.