3

I want to print an html file to the default printer with powershell. So lets say I have the file c:\test.html with the text:

<html>
<p> hello <b>world</b></p>
<html>

How could I print test.html to the default printer?

Thank you in advance.

Artur Carvalho
  • 6,901
  • 10
  • 76
  • 105

1 Answers1

6
get-content c:\test.html  | out-printer

Print to default printer.

Edit:

if you need print rendered htlm page:

$ie = new-object -com "InternetExplorer.Application"
$ie.Navigate("c:\test.html")
$ie.ExecWB(6,2)

Edit after comments:

I can run this in a testprint.ps1 file:

$ie = new-object -com "InternetExplorer.Application"
$ie.Navigate("c:\test.html")
while ( $ie.busy ) { Start-Sleep -Seconds 3 }
$ie.ExecWB(6,2)
while ( $ie.busy ) { Start-Sleep -Seconds 3 }
$ie.quit()
Jimadine
  • 998
  • 13
  • 26
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Thank you Christian! Almost perfect. I see that I stay with IE running, should I run a "close" command? I tried $ie.Quit() and $ie.Close() but had no luck. Any ideas? – Artur Carvalho Aug 25 '11 at 12:28
  • To dipose the IE session open as $ie you have to call $ie.quit(). This not close other IE sessions open. Can you try again closing all other IE session? – CB. Aug 25 '11 at 12:36
  • If I put the quit it does not print. Tried this: "While ($ie.Busy) { Start-Sleep -Milliseconds 400 } $ie.Quit()" but it doesn't print. – Artur Carvalho Aug 25 '11 at 12:38
  • 1
    Did a harcode 5 seconds sleep and it works. Need to do a wait in the main thread or something like that. – Artur Carvalho Aug 25 '11 at 12:40
  • Glad it helped! see OLECMDID, OLECMDEXECOPT on msdn, are the values of ExecWB member! – CB. Aug 25 '11 at 13:35
  • I get `Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100 (DRAGDROP_E_NOTREGISTERED)) At line:4 char:1 + $ie.ExecWB(6,2);` – Yoda Jun 06 '19 at 11:34
  • @Yoda I saw that error. I fixed it by removing the `while`s, so just `Start-Sleep -seconds 3` – Jimadine Feb 02 '22 at 15:16
  • Also, if you get an `The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)`, this solution worked for me: https://stackoverflow.com/a/721519/1754517 – Jimadine Feb 02 '22 at 16:32