Windows – How to Print to a Local Printer from the Command Prompt

command lineprintingwindowswindows 8.1

I have the following scenario:

I have a local printer connected to my Windows 8.1 PC and I would like to print several documents (files with the .txt, .pdf and .doc extensions), from the Command Prompt.

I have tried using the print command documented here. However, it doesn't work. I get no stuff being printed and an error message that says:

"Unable to initialize device DeviceName"

It doesn't matter if I specify the port where the printer is connected or the PC name followed by the printer name.

Is this command still supported in Windows 8.1? How can I solve this problem?

Best Answer

Here's a way to do it with VBScript. Based on an answer provided by Jobbo.

The catch is, you must have a program registered as the default handler. In other words, if you just type the name of a file from the command line, it should open in the appropriate program. For example, if you pass the argument of "file.pdf" you must have a PDF viewer installed. If you want to print a Word document, you must have Word (I think a viewer should work, but I didn't test that) installed.

One more thing, some programs leave a window open (Adobe Acrobat Reader X) after the document prints. You could add logic to the script to close it, but I'll leave that up to you.

To use, type in cscript /nologo <name_of_script.vbs> <name_of_file_to_print> where <name_of_script.vbs> is the name of the program you save this under, and <name_of_file_to_print> is the name of the file you would like to print. If the path contains spaces, enclose the argument in quotes.

Option Explicit

Dim shl, objFS
Dim fldr
Dim files,file
Dim file_to_print,wrk_folder

Set shl = CreateObject("Shell.Application")
Set objFS = CreateObject("Scripting.FileSystemObject")

if not wscript.Arguments.Count = 1 then 
    wscript.echo "Missing parameter!"
    wscript.quit
end if

file_to_print = wscript.arguments(0)
file_to_print = objFS.GetAbsolutePathName(file_to_print)
wrk_folder = objFS.GetParentFolderName(file_to_print) & "\"

wscript.echo "Argument passed: " & wscript.arguments(0)
wscript.echo "Absolute file path: " & file_to_print
wscript.echo "Work folder: " & wrk_folder & vbcrlf

if objFS.FileExists(file_to_print) then
    Set fldr = shl.Namespace(wrk_folder)
    Set files = fldr.Items

    For Each file in files
      If LCase(file.Path) = LCase(file_to_print) Then
        file.InvokeVerbEx("Print")
      End If

    Next
end if

Set shl = Nothing
Set fldr = Nothing
Set files = Nothing
Set objFS = Nothing

WScript.Quit
Related Question