Windows PowerShell ISE provides the facility to write scripts and execute them to perform various actions. PowerShell ISE is an extension to the essential command-line tool PowerShell. It comes with built-in support for Microsoft Windows. However, you can install it for Linux distros and macOS, as well. PowerShell provides a simple command interface for execution, whereas ISE has a more interactive and detailed interface and executes complex and larger scripts. PowerShell and PowerShell ISE can be related to two-word processors “Notepad” and “Word“.

In this post, we will demonstrate various methods to print the file using PowerShell ISE scripts.

How to Print File Through PowerShell Script

Open PowerShell ISE by following the steps given below.

Click on the Search icon on the Taskbar, and search for “PowerShell ISE“. Then, click on the name and open it:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-555.png" data-lazy- height="670" src="data:image/svg xml,” width=”670″>

The scripting pane and output pane of the PowerShell ISE will be displayed:

First, save your untitled script with the preferred name. Then, press “ctrl s” to save your script. We have saved the script with the “test.ps1” name.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-556.png" data-lazy- height="241" src="data:image/svg xml,” width=”758″>

If you do not know the cmdlet used for print, write the following line in your script and press “ctrl s” to save the script. This Get-Command will print all the functions, cmdlets, and applications in which the “print” word exists:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-557.png" data-lazy- height="110" src="data:image/svg xml,” width=”489″>

Moreover, to run the script from the output pane, you must redirect your terminal to the folder where the script is saved. We have kept the script in C:usersadnandownloads.

Once you have saved the script, go to the output pane and run the script, as shown in the image below. Note, the PowerShell cmdlet for print is “Out-Printer“.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-558.png" data-lazy- height="569" src="data:image/svg xml,” width=”907″>

The “Out-Printer” cmdlet of the PowerShell will only send data to your printer.

Now, we will move towards printing a file using this “cmdlet“.

I have created a .txt file and saved it as “printfile.txt” in the same directory where the “print.ps1” script is saved.

You have to use the “Get-Content” cmdlet and pipe it with “Out-Printer” to get the print of the content inside “printfile.txt“. The “Get-Content” will take the content from the file and send it to the Out-printer. Furthermore, the Out-Printer will send the information to the printer. The command to print the file using PowerShell script is shown below:

Get-Content -Path ./printfile.txt | Out-Printer

Copy and paste the command in your script. You can create a script with your path of a file to print. We have made another script, “getprint.ps1,” in the same directory:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-559.png" data-lazy- height="118" src="data:image/svg xml,” width=”585″>

When you run this script, it will print the content of the file. If your machine is not connected to the printer, it will redirect you to save your file as a PDF, as shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-560.png" data-lazy- height="599" src="data:image/svg xml,” width=”1087″>

Parameters

Parameters that are supported by “Out-Printer“:

– InputObject: You can save the file’s content in a variable and then pass that variable to “Out-Printer“.

– Name: The print cmdlet “Out-Printer” will print the file using a default printer. However, if you want to print the file on a specific server, then this -Name parameter is used.

We will discuss both parameters in the latter part of this guide.

InputObject Parameter

You can print the content of the file using the “-InputObject” parameter of “Out-Printer“. We will get the content of “printfile.txt” and will save it in a variable. After that, Out-Printer will get the information from the variable and sends it to the printer.

We have created another script and saved it as “inputobj.ps1“. Write the following code in the script to get the content of “printfile.txt” in a variable “$P“. Moreover, the second line of the code shows that “-InputObject” passes “$P” variable to “Out-Printer“, and it will send the data to the printer:

$P = Get-Content -Path printfile.txt

Out-Printer -InputObject $P

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-561.png" data-lazy- height="143" src="data:image/svg xml,” width=”465″>

Execute the script in the output pane. Once the script is executed successfully, it will print the selected file:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-562.png" data-lazy- height="238" src="data:image/svg xml,” width=”503″>

Name Parameter

If you want to print the file using another printer that is not your default, you need that printer’s name or the printer’s location. In case you do not know the name or location of the printer, click on the search icon and search for “Printers & scanners“:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-564.png" data-lazy- height="670" src="data:image/svg xml,” width=”785″>

Open the “Printers & scanners“. You will get the list of added printers and scanners. Click on the printer of your choice and select “Manage“:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-567.png" data-lazy- height="485" src="data:image/svg xml,” width=”520″>

Once you clicked on “Manage“, it will open another window. Select “Printer Properties” from that window:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-569.png" data-lazy- height="579" src="data:image/svg xml,” width=”534″>

After that, you will get the name and location of the printer as shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-570.png" data-lazy- height="510" src="data:image/svg xml,” width=”463″>

You can use the name or location of the printer.

The following command will print the file:

Get-Content -Path ./printfile.txt | Out-Printer -Name “HP155B02 (HP Smart Tank 510 series)”

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-573.png" data-lazy- height="143" src="data:image/svg xml,” width=”923″>

Now, run the script in PowerShell ISE to print the file on the printer with the specified name:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-574.png" data-lazy- height="272" src="data:image/svg xml,” width=”947″>

Conclusion:

PowerShell ISE allows the users to interact with the different tasks of Windows using the command line. Printing has emerged as a widely used action of most computer users, either printing documents or printing photos. In this article, we have demonstrated the ways to print a file using PowerShell ISE scripts. The Out-Printer cmdlet of PowerShell ISE enables you to print the file via ISE scripts. Moreover, we have shown the use of various parameters like “-InputObject” and “-Name“.