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.

1
  • I assume you are interested in printing the rendered output rather that the html source code. Commented Aug 25, 2011 at 11:21

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

8 Comments

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?
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?
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.
Did a harcode 5 seconds sleep and it works. Need to do a wait in the main thread or something like that.
Glad it helped! see OLECMDID, OLECMDEXECOPT on msdn, are the values of ExecWB member!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.