Average Rating:

4.50 by 2 users.

26 Downloads, 82 Views

1 Comment(s)

Out-TempFile

By: vbscripter 18 Apr 2011 05:32 AM UTC in the category: Advanced Event 10

Tags:

Description:

This function saves data to a temporary file and returns path to it. The function accepts piped content and has comment-based Help.
<#
.SYNOPSIS
Saves data to a temporary file and returns path to it.
.DESCRIPTION
Saves data to a temporary file.
The temporary file has a temporary name, and resides in the temporary folder.
The function returns path to the temporary file.
.PARAMETER Preview
Displays the content of the temporary file in Notepad
.PARAMETER Encoding
Specifies encoding type. Possible values: "ASCII", "Unicode". Default value is "Unicode".
.EXAMPLE
Get-Date | Out-TempFile
Saves current date to a temporary file.
.EXAMPLE
Get-Date | Out-TempFile -Preview
Saves current date to temporary file and opens file in Notepad.
.EXAMPLE
Get-Date | Out-TempFile -Encoding ASCII
Saves current date to temporary file using ASCII encoding type.
#>

function Out-TempFile([switch] $Preview, [string] $Encoding = "Unicode")
{
   
   begin
   {
       Write-Verbose "Creating temporary file..."
       $FilePath = [System.IO.Path]::GetTempFileName()
       Write-Debug ("Temporary file path is '" + $FilePath + "'")
   }
   end
   {
        Write-Verbose "Writing to temporary file..."
       $input | Out-File $FilePath -Encoding $Encoding
       if ($preview)
       {
        Write-Verbose "Launching 'notepad' to display the temporary file..."
        notepad $FilePath
       }
       $FilePath
   }
}
Top

Comments:

4/24/2011 7:38 PM
Good simple precise script, that does what it is supposed to.