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.
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.
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.
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
yeah but my submenus are crowded too now 😉
Ahhhh. In that case I think it is time to invest in a commercial IDE.