Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun Add A Print Menu to the PowerShell ISE

Posted on September 9, 2011September 9, 2011

I spend a fair amount of time in the PowerShell ISE. One task that I find myself needing, especially lately, is the ability to print a script file. I'm sure you noticed there is no Print menu choice. So I decided to add my own to the ISE.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Printing a file in PowerShell is actually not that difficult: get the text content and send it to Out-Printer.

[cc lang="PowerShell"]
Get-Content myscript.ps1 | Out-Printer
[/cc]

This will send the file to the default printer. In the ISE, you could use a command like this:

[cc lang="PowerShell"]
Get-Content $PSISE.CurrentFile.FullPath | out-printer
[/cc]

One issue I've found with Out-Printer is that the formatting sometimes isn't as nice I'd like; I end up with a variable width font and not a fixed width. Another way, though which always guarantees a fixed width font is to use Notepad. Fortunately, you can print with Notepad right from a command prompt.

[cc lang="PowerShell"]
notepad /p c:\scripts\myscript.ps1
[/cc]

This will print to the default printer using whatever last settings you used for Notepad. I tend to bump the font size. See where I'm going with this?

[cc lang="PowerShell"]
Notepad /p $PSISE.CurrentFile.FullPath
[/cc]

That's really all there is to it, but I hate typing any more than I have to. So I wrote a simple function that I can use in the ISE.

[cc lang="PowerShell"]
Function Print-ISEFile {

Param([string]$path=$PSISE.CurrentFile.FullPath)

Start-Process -filepath Notepad.exe -ArgumentList "/p",$path -WindowStyle Hidden

<# this is an alternative way using the default printer get-content -Path $path | out-printer #>

} #end function
[/cc]

In my final function I decided to use Start-Process to hide as much of the popup windows as I could. Once the function is loaded, I can run the function and it will print the current file. But let's take this one step further and add the function as a menu option to the AddOns menu.

[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Print Script",{Print-ISEFile},"CTRL+ALT+P") | Out-Null
[/cc]

This will add a menu choice called Print Script with a keyboard shortcut of Ctrl+Alt+P. I dot source the script file in my PowerShell ISE profile and I'm good to go.

Download Print-ISEFile.


Behind the PowerShell Pipeline

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Friday Fun Add A Print Menu to the PowerShell ISE”

  1. Vincent says:
    September 12, 2011 at 3:03 am

    Handy ! As always.

    I’m afraid I know the answer beforehand, but are we really restricted to the “Add-Ons” menu in ISE ? It’s getting pretty crammed up there.

    BTW thanks for the tips on James Rollins & Jeff Long, as well. You dropped those names a while back, and being a fan of Preston/Child already, I checked them out.

    1. Jeffery Hicks says:
      September 12, 2011 at 5:58 am

      I know what you mean. There is no other menu, but you can create sub menus.


      #create a custom sub menu
      $jdhit=$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("JDHIT",$null,$null)

      #add my menu addons
      $jdhit.submenus.Add("Save File ASCII",{$psISE.CurrentFile.Save([Text.Encoding]::ASCII)}, $null) | out-null
      $jdhit.submenus.Add("Convert to text file",{ConvertTo-TextFile}, "ALT+T") | out-null

  2. Vincent says:
    September 13, 2011 at 2:58 am

    yeah but my submenus are crowded too now 😉

    1. Jeffery Hicks says:
      September 13, 2011 at 6:16 am

      Ahhhh. In that case I think it is time to invest in a commercial IDE.

  3. Pingback: Print from PowerShell’s Integrated Scripting Environment « Unlock-PowerShell

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2026 The Lonely Administrator | Powered by SuperbThemes!
%d