Print queue

Hi everyone,

How I can unshared or delete server print queue and ip_port assign to this print queue

this is my code:

$queueserver = “server1”
$PrinterListToUnshare = Get-Content “C:\Support\unshareServerPrintQueue.txt”
Foreach ($PrinterToUnshare in $PrinterListToUnshare) {
Get-WmiObject -ComputerName $queueserver -Class Win32_Printer -Filter “Name = ‘$PrinterToUnshare’” |
ForEach-Object {$.shared = $false; $.Put()}
}

$queueserver = “server1”
$PrinterListToUnshare = Get-Content “C:\Support\deletServerPrintQueue.txt”
Foreach ($PrinterToUnshare in $PrinterListToUnshare) {
Get-WmiObject -ComputerName $queueserver -Class Win32_Printer -Filter “Name = ‘$PrinterToUnshare’” |
ForEach-Object {$_.Delete()}
}

both part work OK
I can unshared or delete print queue,
but how I can delete ip_port to each print queue

Thanks.

Check out the Win32_TCPIPPrinterPort class and this article: [url]How can i delete a win32_tcpipprinterport with powershell!!?

Hi Jack,

Thx for this link
but I need help how modify this script

function Remove-PrinterAndPort{
Param( $printername )
$printer=gwmi win32_Printer -filter “name=‘HPDJ600’”
$printer.Delete()
$port=gwmi win32_tcpipprinterport -filter “name=‘$($printer.portname)’” -enableall
$port.Delete()
}

this script work for one printer.

I modified it to run from server and take data from list

function Remove-PrinterAndPort{
Param( $printername )
$servername =“servername”
@printerlist = Get-Content “c:\script\delete.txt”

Foreach($PrintDelete in $printerlist)
$printer=gwmi win32_Printer -ComputerName $servername -filter “name=‘$PrintDelete’”
$printer.Delete()
$port=gwmi win32_tcpipprinterport -ComputerName $servername -filter “name=‘$($printer.portname)’” -EnableAllPrivileges
$port.Delete()
}

}

it’s working, but give me error “calling “Delete” with “0” arguments” line 12 this is $port.Delete()

what I doing wrong

Thanks.

Any chance a single port is associated with multiple printers? If a port is still being utilized by a printer, the port can’t be deleted.

Hi Jason,

I tested many times
even if many print queue was assign with one port
exp: 11111, 2222, 33333 - 192.168.1.25

if I have in my list 1111,2222
it will delete this queue, but 33333 will stay

this is OK, this is what I need

but why it throwing error, what wrong?

Thanks.

Here’s a function from a larger script I wrote years ago for managing printers. This function removed network printers from the local Win7 computer running PS v2, and worked well throughout quite a bit of testing. I’m not sure what’s going on with your code, but maybe this can help.

Function Remove-NetworkPrinters
{
    $Ports = Get-WmiObject -Class Win32_TCPIPPrinterPort
    if (-NOT $Ports)
    {
        Write-Log "No TCPIP Ports found"
        return
    }
    
    $Printers = Get-WmiObject -Class Win32_Printer
    if ( $Printers )
    {
        $NetworkPrinters = @()

        foreach ( $Port in $Ports )
        {
            foreach ( $Printer in $Printers )
            {
                if ( $Printer.PortName -eq $Port.Name )
                {
                    $NetworkPrinters += $Printer
                }
            }
        }
        
        $NetworkPrinters | foreach {
            Write-Log "Deleting Printer: $($_.Name)"
            $_.Delete()}
    }
    else
    {
        Write-Log "No TCPIP Printers Found"
    }
    
    $Ports | foreach {
        Write-Log "Deleting Port: $($_.Name)"
        $_.Delete()}
}

Thank you Jason
will try and let you know how it work
how I can add in you script
computername and list from txt file

Thanks.

hey Jason,

can I see your larger script for managing printers
or you can mail me: sbrad725@gmail.com

Thanks.

Here’s the code. It’s very specific to our environment and problem. We’re not even using this anymore. Now local IT support at our locations just create their own printerExport files with PrintBrm.exe and deploy them with the Print Management tool or a PowerShell function. It’s hard to make sense of it without the documentation I wrote to go with it, but I’ve put the first couple paragraphs here.

Background Information
The objective of AutoPrinter is to reduce printer management workload on Field Services without creating additional workload for Operations
personnel.
AutoPrinter is a method for automatically installing printers on workstations based on the subnet. Printer export files are generated using the
PrintBrm utility on Windows 7. These files are placed on server shares within subdirectories corresponding to particular subnets. A computer startup
script is distributed to clients through domain group policy. The startup script checks to see if the computer’s subnet has changed, or if the printer
export files have changed. If the subnet has changed, all printers for that subnet are installed. If any new or updated files are located in the subnet
directory, those files are installed. Network and file information is stored in the registry, and other values are implemented to allow IT to remove
existing printers, force a reinstall, or disable the script and prevent any changes.
Overall Architecture
Windows 7 has a Print Management utility with the capability to export installed printers to a single printerExport file. This file contains all the
necessary driver binaries, port configuration and even driver-specific options such as whether or not the duplexer is installed. This file can then be
taken to another Windows 7 computer and imported. Several LAN Administrators started using this technique after it was demonstrated in the Tiger
Team training. It is also shown in one of the PowerShell training videos.
The AutoPrinter script takes this concept a step further by having computers automatically download and install printerExport files that are relevant
for their location.
Locations will be setup in and as repositories for the printerExport files. These locations will contain a “GLOBAL” directory and
subnet directories.
The global directory will contain only printerExport files with drivers that should be installed to all computers, such as the Lexmark Universal driver.
The subnet directories will contain any printerExport files for printer configurations specific to that particular subnet. By splitting the binary drivers
from the configurations in this way, the storage requirements and network utilization is dramatically decreased. The printerExport files will be
generated by LAN Admins and the Sector Admins will be responsible for reviewing and copying the files to the correct location on the share.
Group Policy will be used to deploy the AutoPrinter script to domain computers as a startup script. Each time the computer boots, it will determine
which subnet it is connected to, map a drive to the repository, then download and install any new or updated printerExport files that apply to its
subnet.
If a directory corresponding to the computer’s subnet is not found, no changes will be made except for setting registry values. No customer impact is
expected on implementation because the Sector Administrators will need to create these subnet directories before any actions are taken. This allows
Field Services to implement on their own schedule.
There are several registry values the AutoPrinter script looks for when it runs. LAN Administrators can customize the behavior of the AutoPrinter
script on a particular computer by modifying these values with the Set-DCMAAutoPrinter script. The “Clean” option will delete any existing printers
or ports, the “Force” option will reinstall all relevant printerExport files, and the “Disable” option allows the script to be turned off on a per-computer
basis.
The script will log its actions to the AutoPrinter_.log file in the C:\Windows\TEMP directory.
There are several use cases that were taken into consideration when designing this script.
No changes should be made to any computers at a site (subnet).
Do not create a directory for the subnet on the share. The script will see there is no directory and not install anything.
Only printer drivers should be installed to computers at a site.
Create a directory for the subnet, but leave it empty. The script will see the directory exists and install the files from the global directory.
Printers should be installed to all computers at a site.
Create a directory for the subnet, and place printerExport files within it. The script will install the global files and files within the subnet
directory.
User is having a problem with their printers and they all need to be reinstalled.
Set Force flag. The next time the script runs, it will force a reinstall of the driver files and printer files from the share. The flag is set back to 0
after the script runs.
Computer has several old printers and ports that need to be removed.
Set Clean flag and Force flag. The script will delete all existing TCP\IP printers, then force the reinstall of driver files and printer files from the
share. Both flags are set back to 0 after the script runs.
Computer is a special case where changes to printers should not be automatically made.
Set Disable flag. The script will see the flag is set and exit before making changes. The flag will remain set through reboots and group policy
updates.

Thank you Jason
thanks for script
it take me time to play with your script
but for now can you help me with how I can add in you script
computername and list from txt file
Function Remove-NetworkPrinters
{
$Ports = Get-WmiObject -Class Win32_TCPIPPrinterPort
if (-NOT $Ports)
{
Write-Log “No TCPIP Ports found”
return
}

$Printers = Get-WmiObject -Class Win32_Printer
if ( $Printers )
{
    $NetworkPrinters = @()

    foreach ( $Port in $Ports )
    {
        foreach ( $Printer in $Printers )
        {
            if ( $Printer.PortName -eq $Port.Name )
            {
                $NetworkPrinters += $Printer
            }
        }
    }

    $NetworkPrinters | foreach {
        Write-Log "Deleting Printer: $($_.Name)"
        $_.Delete()}
}
else
{
    Write-Log "No TCPIP Printers Found"
}

$Ports | foreach {
    Write-Log "Deleting Port: $($_.Name)"
    $_.Delete()}

}

Have you looked in the TechNet Script Center Repository? https://gallery.technet.microsoft.com/scriptcenter

There might be something there that’s closer to what you want.

Thank you Jason