# Print queue

**URL:** <https://forums.powershell.org/t/print-queue/3712>\
**Category:** PowerShell Help\
**Created:** [February 5, 2015, 6:43am UTC](https://forums.powershell.org/t/print-queue/3712 "2015-02-05T06:43:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 5, 2015, 6:43am UTC](https://forums.powershell.org/t/print-queue/3712/1 "2015-02-05T06:43:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jack-neff](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jack-neff](https://forums.powershell.org/u/jack-neff)\
**Post date:** [February 5, 2015, 1:41pm UTC](https://forums.powershell.org/t/print-queue/3712/2 "2015-02-05T13:41:06Z")

</div>

Check out the Win32\_TCPIPPrinterPort class and this article: [url][How can i delete a win32\_tcpipprinterport with powershell!!?](https://social.technet.microsoft.com/Forums/scriptcenter/en-US/b6696958-a64f-4947-bd56-72abb8aa64bb/how-can-i-delete-a-win32tcpipprinterport-with-powershell?forum=ITCG%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 9, 2015, 5:02am UTC](https://forums.powershell.org/t/print-queue/3712/3 "2015-02-09T05:02:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jhofferle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jhofferle/32/804_2.png) [@jhofferle](https://forums.powershell.org/u/jhofferle)\
**Post date:** [February 10, 2015, 12:50am UTC](https://forums.powershell.org/t/print-queue/3712/4 "2015-02-10T00:50:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 10, 2015, 1:39am UTC](https://forums.powershell.org/t/print-queue/3712/5 "2015-02-10T01:39:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jhofferle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jhofferle/32/804_2.png) [@jhofferle](https://forums.powershell.org/u/jhofferle)\
**Post date:** [February 10, 2015, 1:50am UTC](https://forums.powershell.org/t/print-queue/3712/6 "2015-02-10T01:50:42Z")

</div>

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()}
}
```

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 10, 2015, 2:18am UTC](https://forums.powershell.org/t/print-queue/3712/7 "2015-02-10T02:18:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 10, 2015, 2:23am UTC](https://forums.powershell.org/t/print-queue/3712/8 "2015-02-10T02:23:14Z")

</div>

hey Jason,

can I see your larger script for managing printers  
or you can mail me: [sbrad725@gmail.com](mailto:sbrad725@gmail.com)

Thanks.

---

<div class="post-metadata">

**Author:** ![jhofferle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jhofferle/32/804_2.png) [@jhofferle](https://forums.powershell.org/u/jhofferle)\
**Post date:** [February 10, 2015, 3:15am UTC](https://forums.powershell.org/t/print-queue/3712/9 "2015-02-10T03:15:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 10, 2015, 6:35am UTC](https://forums.powershell.org/t/print-queue/3712/10 "2015-02-10T06:35:34Z")

</div>

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()}

```

}

---

<div class="post-metadata">

**Author:** ![jhofferle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jhofferle/32/804_2.png) [@jhofferle](https://forums.powershell.org/u/jhofferle)\
**Post date:** [February 10, 2015, 7:14am UTC](https://forums.powershell.org/t/print-queue/3712/11 "2015-02-10T07:14:27Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 10, 2015, 9:26am UTC](https://forums.powershell.org/t/print-queue/3712/12 "2015-02-10T09:26:12Z")

</div>

Thank you Jason

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/print-queue/3712/13 "2024-05-16T20:46:24Z")

</div>


