Out-Notepad

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.

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

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 PowerShell, PowerShell v2.0, Scripting and tagged , , , . Bookmark the permalink.

6 Responses to Out-Notepad

  1. John Gullion says:

    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!

    • Jeffery Hicks says:

      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:

    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.

  3. alex says:

    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

    • Jeffery Hicks says:

      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.