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

Out-Notepad

Posted on October 6, 2009

Maybe this isn’t the most earth shattering PowerShell function you’ll ever come across, but it saves me a few keystrokes. There are times when I want to see the results of  PowerShell expression but the console output is insufficient. I want to see the results in a text file opened in Notepad so I can easily scroll, search or whatever.

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!

Directing output to a text file with Out-File is trivial and all I have to do is open the file in Notepad. But that takes too much work.  I have to come up with a file name, take some time to open Notepad, clean up the file if I don’t need it.  I mean..really…I’m trying to work here. My solution is a  short function called Out-Notepad.

Function Out-Notepad {

#this function is designed to take pipelined input

#example: get-process | out-notepad

 

    Begin {

        #create the FileSystem COM object

        $fso=new-object -com scripting.filesystemobject

        $filename=$fso.GetTempName()

        $tempfile=Join-Path $env:temp $filename

        

        #initialize a placeholder array

        $data=@()

    } #end Begin scriptblock

    

    Process {

        #add pipelined data to $data

        $data+=$_

    } #end Process scriptblock

    

    End {

        #write data to the temp file

        $data | Out-File $tempfile

        

        #open the tempfile in Notepad

        Notepad $tempfile

        

        #sleep for 5 seconds to give Notepad a chance to open the fi

        sleep 5

        

        #delete the temp file if it still exists after closing Notepad

        if (Test-Path $tempfile) {del $tempfile}

    } #end End scriptblock

 

} #end Function

The function is designed to take pipelined input and will work with any PowerShell version.

The code in the Begin scriptblock executes once before any pipelined input is processed. Here I’m creating the FileSystemObject like I used in VBScript. This object has a handy method called GetTempName() which returns, naturally, a temporary file name like radF913E.tmp. I use the Join-Path cmdlet to construct a compl;ete filename using the %TEMP% environmental variable. The last part of the script block is to initialize an array.

This array is used in the Process scriptblock to keep a copy of every object that is piped in to the function. After every object has been processed, the End script block executes. The $data array is piped to Out-File using the temporary file name. Notepad is then launched to open the file. At this point I have everything I need. The script sleeps for 5 seconds which should be plenty of time for Notepad to open the temp file and load it into memory. Once Notepad is running I can delete the temp file. Most of the time when I’m finished I’m simply going to close Notepad. If I need to save the information then I’ll simply save the file with a new name anyway to a different location. I’m expecting that 9 times out of 10 all I need is a temporary view.

The script file that loads the function also creates an alias of on so that my command line work can be very quick:

PS C:\> ps | on


Behind the PowerShell Pipeline
Download the function here.

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

6 thoughts on “Out-Notepad”

  1. John Gullion says:
    October 6, 2009 at 3:17 pm

    Thanks Jeff, great script! You published it just as I was trying to get the placeholder array thru my little head on another script. Thanks!

    1. Jeffery Hicks says:
      October 6, 2009 at 3:19 pm

      Glad it helped. I try to remind myself that even if the script or function itself isn’t useful, perhaps a technique or implementation example might be.

  2. Larry Weiss says:
    October 6, 2009 at 5:46 pm

    I like your function Out-Notepad but I was a little confused about the comment

    #delete the temp file if it still exists after closing Notepad

    A simple

    #delete the temp file

    would have been a better comment since Notepad will certainly still be open at that time.

    1. Jeffery Hicks says:
      October 6, 2009 at 6:33 pm

      Sorry for the confusion. I moved some things around and never updated the comment.

  3. alex says:
    October 7, 2009 at 3:22 am

    And the alias (or function) by itself opens a blank notepad (or alternate editor).

    Simplify?
    $TempFile = [System.IO.Path]::GetTempFileName()

    Complexify (alternate editor, not in path)
    ## Get the EXE path
    If ($ENV:PROCESSOR_ARCHITECTURE -eq “AMD64″) { $nPlusPlus = (Get-ItemProperty -path HKLM:\Software\Wow6432Node\Notepad++).”(Default)” }
    If ($ENV:PROCESSOR_ARCHITECTURE -eq “X86″) { $nPlusPlus = (Get-ItemProperty -path HKLM:\Software\Notepad++).”(Default)” }
    ## Join path and EXE. Execute.
    & “$nPlusPlus\Notepad++.exe” $TempFile

    1. Jeffery Hicks says:
      October 7, 2009 at 9:13 am

      I knew there was another way to get a temp file name but in my viral induced haze I couldn’t come up with it. I also debated about other editors but I figured people could simply replace Notepad with the name and path to their editor of choice.

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

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