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: Send PowerShell ISE Content to Word

Posted on March 13, 2015March 16, 2015

geekYesterday on Facebook, Ed Wilson was lamenting about confusion of keyboard shortcuts between PowerShell and Microsoft Word. I've run into the same issue. Muscle memory is strong. Then the discussion turned to getting content from the PowerShell ISE into a Word document. I humorously suggested we had a plugin and it had a Ctrl+C keyboard shortcut. Then I thought, why not make this even easier!

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!

So I put together a quick function for the PowerShell ISE.

Function Send-ToWord {
[cmdletbinding()]
Param(
[ValidatePattern("\S+")]
[string[]]$Text = $psise.CurrentFile.Editor.SelectedText)

If (($global:word.Application -eq $Null) -OR -NOT (Get-Process WinWord)) {
    #remove any variables that might be left over just to be safe
    Remove-Variable -Name doc,selection -Force -ErrorAction SilentlyContinue

    #create a Word instance if the object doesn't already exist
    $global:word = New-Object -ComObject word.application

    #create a new document
    $global:doc = $global:word.Documents.add()

    #create a selection
    $global:selection = $global:word.Selection

    #set font and paragraph for fixed width content
    $global:selection.Font.Name = "Consolas"
    $global:selection.font.Size = 10
    $global:selection.paragraphFormat.SpaceBefore = 0
    $global:selection.paragraphFormat.SpaceAfter = 0

    #show the Word document
    $global:word.Visible = $True    
}

#insert the text
$global:selection.TypeText($text) 
#insert a new paragraph (ENTER)  
$global:selection.TypeParagraph()

} #end Function

This function will paste any selected text from the ISE into a Word document. The first time you run the function, PowerShell will create a Word document and format it for fixed width text. It will then insert your text and a new paragraph marker. The next time you run the function, it should detect that you have a document open and re-use the existing variables. The Word document will be visible so you can edit it further and save it. If you move the cursor around in the document, any new content you insert will go there.

To make this easy to use, insert this function into your PowerShell ISE profile script and add a menu item with a keyboard shortcut.

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Send to Word",{Send-ToWord},"Ctrl+Alt+W") | Out-Null

Send-ToWord

Now, I can select code from the ISE script pane and send it to Word with a quick key combination. Have fun and enjoy your weekend.

Update: I posted another version that includes an option to copy and paste as colored code.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

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

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