Hey, Scripting Guy! Weekend Scripter: How Can I Create, Display, and Then Delete a Temporary Text File?

ScriptingGuy1

  Microsoft Scripting Guy Ed Wilson here, it’s a good thing I enjoy getting up early on weekends. To me, because I get up early during the week, it makes sense to get up early on the weekend and get busy doing things that I want to do. One of the things I like to do early on weekend mornings is hang out on Facebook and Twitter for a little while as I wait for my English Breakfast tea to steep, and as I wait for the Irish steel cut oats to cook. One of the things I wanted to look at this morning arose because of a presentation I made to the Philadelphia Area Exchange Users Group this week. I showed them a script I had written several years ago that creates a temporary text file, writes process information to the temporary text file, displays the information in Notepad, and then deletes the temporary text file when you close Notepad. It is a pretty cool script, and I posted it to Scripting Guys Script Repository that night after the user group meeting. I thought the script would be more useful if I modified it to accept piped input. I got a little carried away, and turned it into an advanced function. The Out-TempFile.ps1 script contains the Out-TempFile advanced function. I added help tags so the function interacts with the Get-Help function. For more information about adding help to a Windows PowerShell script, refer to the articles from Help week. Out-TempFile.ps1

Function Out-TempFile
{
<#
.Synopsis
Displays pipelined text in a temporary file. The temp file
is displayed in Notepad. When Notepad is closed, the temp
file is deleted. If you wish to save the information that 
is displayed in Notepad, use the saveas feature from Notepad.
.Example
“string”
| Out-TempFile
Displays the word string in a temporary file via Notepad.
When Notepad is closed, the temporary file that contained
the word “string” is deleted.
.Example
Get-Process | Out-TempFile
Displays process info in a temporary file via Notepad.
When Notepad is closed, the temporary file that contained
the process information is deleted.
.Example
Out-TempFile -in (get-service)
Displays service info in a temporary file via Notepad.
When Notepad is closed, the temporary file that contained
the process information is deleted.
.Parameter in
Data to display in a temporary file
.Inputs
[psobject]
.Outputs
[psobject]
.Notes
NAME: Out-TempFile
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 7/15/2010
WES-07-18-2010
KEYWORDS: WeekEnd Scripter, Function, Getting Started
.Link
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#>
#Requires -Version 2.0
[CmdletBinding()]
param(
[Parameter(Mandatory = $True,valueFromPipeline=$true,Position=0)]
[psobject]
$in
) #end param
BEGIN
{ $tempFile = [io.path]::GetTempFileName() }
PROCESS
{ out-file -filePath $tempFile -InputObject $in -Append `
-Width 300 -Encoding unicode }
END
{ Notepad $tempFile | Out-Null
Remove-Item $tempFile }
} # end function out-tempfile

After creating the help tags, I use the CmdletBinding attribute. This causes the function to bind parameters in the same way that a Windows PowerShell cmdlet binds the parameters. I then create the input parameter. To enable piped input to the function, it is necessary to specify the valueFromPipeLine parameter. The other parameters make the –in parameter mandatory and set it in the first position. The [psobject] type constraint makes the input an object, and not merely a string. The parameter section and CmdletBinding attribute are shown here:  

[CmdletBinding()]

param(

[Parameter(Mandatory = $True,valueFromPipeline=$true,Position=0)]

[psobject]

$in

) #end param

The first thing I want to do when the function is called is create the temporary file name. To do this, I use the GetTempFileName static method from the .NET Framework io.path class. I really do not need to know the temporary file name because I store it in the $tempFile variable. This variable will be used when creating and deleting the file. The Begin section of the advanced function defines actions that are performed once. This is shown here:  

BEGIN

{ $tempFile = [io.path]::GetTempFileName() }

The Process section is run for each item that comes through the pipeline. The Out-File cmdlet is used to create the temporary text file and to append data to the file. I set the width to 300 characters and the encoding to Unicode. The Process block is shown here:  

PROCESS

{ out-file -filePath $tempFile -InputObject $in -Append `

-Width 300 -Encoding unicode }

There are two things that remain to be accomplished. The first is to display the temporary file in Notepad; this is very convenient because the user’s temporary folder is cumbersome to navigate to. In addition, many programs have a nasty habit of leaving their temp files in the temp folder, and therefore spotting my specific temporary file gets to be difficult. It is much faster to have Notepad display it for me. After I am finished reading the file, I like to remove the temporary file. To do this, I pipe the Notepad command to the Out-Null cmdlet. This pauses execution of the script. When Notepad is closed, execution resumes and the Remove-Item cmdlet is used to delete the temporary file. The end section of the Out-TempFile function is shown here:  

END

{ Notepad $tempFile | Out-Null

Remove-Item $tempFile }

When the function is called, after you have run the script to load the function (in either the ISE or by dot-sourcing the script into the Windows PowerShell console), the function pauses execution while the temporary text file is displayed, as shown in the following image. Image of paused function as temporary text file is displayed Join us tomorrow when we will begin looking at the Active Directory cmdlets that ship with Windows Server 2008 R2. We would love for you to follow us on Twitter or Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.  

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon