# Printer Install Script Hangs

**URL:** <https://forums.powershell.org/t/printer-install-script-hangs/5850>\
**Category:** PowerShell Help\
**Created:** [February 24, 2016, 6:11am UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850 "2016-02-24T06:11:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian-caine](https://avatars.discourse-cdn.com/v4/letter/i/50afbb/32.png) [@ian-caine](https://forums.powershell.org/u/ian-caine)\
**Post date:** [February 24, 2016, 6:11am UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850/1 "2016-02-24T06:11:02Z")

</div>

I have just starting trying to teach myself Powershell and I am trying to write a script to use at work to install network printers. The script does work in that the printer is installed but it hangs at the add\_printer stage before the $outputBox.text= “Printer with port $var installed” line. The printer is installed but the box never goes away and I have to kill Powershell ISE to get rid of it.  
Is there a way to end the install line to get it to move on ?

Many thanks,  
Ian

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)  
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

$Form = New-Object System.Windows.Forms.Form  
$Form.Size = New-Object System.Drawing.Size(600,400)

############################################## Start functions

function addprinter {  
$var=$InputBox.text;  
pnputil.exe -a “D:\Powershell\Printer\pcl6-x64\hpbuio170l.inf” #get driver  
add-PrinterDriver -name “hp universal printing pcl 6” #install driver  
Add-PrinterPort -Name $var -printerhostaddress $var  
**add-printer -name “HP LJ 400 $var” -portname $var -drivername “hp universal printing pcl 6”  
$outputBox.text= “Printer with port $var installed”**  
} #end addprinter

############################################## end functions

############################################## Start text fields

$InputBox = New-Object System.Windows.Forms.TextBox  
$InputBox.Location = New-Object System.Drawing.Size(20,100)  
$InputBox.Size = New-Object System.Drawing.Size(150,20)  
$Form.Controls.Add($InputBox)

$objLabel = New-Object System.Windows.Forms.Label  
$objLabel.Location = New-Object System.Drawing.Size(10,50)  
$objLabel.Size = New-Object System.Drawing.Size(280,20)  
$objLabel.Text = “Please enter the information in the space below:”  
$Form.Controls.Add($objLabel)

$outputBox = New-Object System.Windows.Forms.TextBox  
$outputBox.Location = New-Object System.Drawing.Size(10,150)  
$outputBox.Size = New-Object System.Drawing.Size(565,200)  
$outputBox.MultiLine = $True  
$outputBox.ScrollBars = “Vertical”  
$Form.Controls.Add($outputBox)

############################################## end text fields

############################################## Start buttons

$Button = New-Object System.Windows.Forms.Button  
$Button.Location = New-Object System.Drawing.Size(400,30)  
$Button.Size = New-Object System.Drawing.Size(75,50)  
$Button.Text = “Install”  
$Button.Add\_Click({addprinter})  
$Form.Controls.Add($Button)

$CancelButton = New-Object System.Windows.Forms.Button  
$CancelButton.Location = New-Object System.Drawing.Size(400,80)  
$CancelButton.Size = New-Object System.Drawing.Size(75,50)  
$CancelButton.Text = “Cancel”  
$CancelButton.Add\_Click({$Form.Close()})  
$Form.Controls.Add($CancelButton)

############################################## end buttons

$Form.StartPosition = “CenterScreen”  
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedToolWindow  
$Form.Text = “Install Printer by Hostname”  
$Form.Topmost = $True

$Form.Add\_Shown({$Form.Activate()})  
[void] $Form.ShowDialog()

PS. this very much a work in progress so I will be adding in some checks to get rid of errors when port is already present etc.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 1, 2016, 3:17am UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850/2 "2016-03-01T03:17:58Z")

</div>

I’d try to break this down and get out of the script context, because it’s possible you’re running into threading limitations.

What happens if you just run Add-Printer interactively from the console to do the same thing? Does it hang then?

---

<div class="post-metadata">

**Author:** ![ian-caine](https://avatars.discourse-cdn.com/v4/letter/i/50afbb/32.png) [@ian-caine](https://forums.powershell.org/u/ian-caine)\
**Post date:** [March 1, 2016, 3:40am UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850/3 "2016-03-01T03:40:34Z")

</div>

Thanks for the reply Don, I actually got around to working this out just yesterday.  
The issue seems to be with the way that the default Powershell ISE runs, if I check the script in PowerGUI it works fine and when I saved the script off and ran it via a batch it also runs fine.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 1, 2016, 4:00am UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850/4 "2016-03-01T04:00:48Z")

</div>

And the ISE _does_ work differently. I _always_ test from the console!

---

<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:42pm UTC](https://forums.powershell.org/t/printer-install-script-hangs/5850/5 "2024-05-16T20:42:45Z")

</div>

Closed on May 16, 2024, 8:42pm UTC


