Friday Fun Add A Print Menu to the PowerShell ISE

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.

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

Get-Content myscript.ps1 | Out-Printer

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

Get-Content $PSISE.CurrentFile.FullPath | out-printer

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.

notepad /p c:\scripts\myscript.ps1

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?

Notepad /p $PSISE.CurrentFile.FullPath

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.

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

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.

$psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Print Script",{Print-ISEFile},"CTRL+ALT+P") | Out-Null

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.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

This entry was posted in Friday Fun, PowerShell ISE and tagged , . Bookmark the permalink.

5 Responses to Friday Fun Add A Print Menu to the PowerShell ISE

  1. Vincent says:

    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.

    • Jeffery Hicks says:

      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:

    yeah but my submenus are crowded too now ;)

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>