Average Rating:

3.00 by 2 users.

30 Downloads, 78 Views

New-TemporaryFile

By: Daniel Richnak 17 Apr 2011 06:08 PM UTC in the category: Advanced Event 10

Description:

Creates a text file in user's TEMP directory. Will write input to this file.
# Daniel Richnak
# daniel@richnak.com
# twitter.com/riknik
#
# #2011SG Advanced Event 10
# Please do not consider me for prizes/daily raffles.

function New-TemporaryFile { 
	param(
	# objects to put into temp text files
	[Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)]
	[Object[]]$Value,
	# file encoding; by using proper enum, get error-checking of input for free (try -encoding UTF8888)
	[Parameter(Position=1, Mandatory=$false)]
	[Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]$Encoding = 
			[Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]"ASCII",
	# whatif - no action will be taken if set
	[Parameter(Position=1, Mandatory=$false)]
	[switch]$WhatIf,
	# view resulting file in Notepad
	[Parameter(Position=2, Mandatory=$false)]
	[switch]$View)
	
	# by using process, this means we'll create a new temp file for **EACH** pipeline input
	PROCESS { 
		# create random file name... use this instead of GetTempFileName() so that 
		#		if -whatif is used, no action is taken (otherwise, the unused temp file would already be generated)
		$tempFilename = [system.io.path]::GetRandomFileName() 
		$tempFilename = join-path $env:temp $tempFilename
		
		# write to temp file - or don't, depending on -whatif switch
		$value | set-content $tempFilename -encoding $encoding -whatif:$whatif
		if ($whatif) { 
			"What if: File encoding: $encoding." | write-host
			"What if: Note that $tempFilename is a temporary file name. `nSubsequent runs of New-TemporaryFile will target different file names." | write-host
		}
		else { # stuff we do only if we actually wrote the file, no -whatif flag
			$tempFilename # return temp file path 
			
			if ($view) {
				invoke-expression "notepad.exe $tempFilename"
			}
		}
	}
	<#
.SYNOPSIS
Creates a text file in user's TEMP directory. Will write input to this file. 

.DESCRIPTION
Creates a text file in user's TEMP directory. Will write input to this file. 

Can accept input from pipeline. Note that a new temporary file will be created for each object in pipeline, not a single file for all objects. 

Will output the path to the temporary file that is created. 

This is a submission for 2011SG Advanced Event 10.

.PARAMETER Value
The object to write to the temporary file. 

Can be input from pipeline. If so, each incoming from pipeline will be written to a separate temporary file. 

If not specified, an empty temporary file will be generated. 

.PARAMETER Encoding
File encoding to set when creating temporary file. 

Default is ASCII. 

.PARAMETER WhatIf
Describes what would happen if you executed the command without actually executing the command.

.PARAMETER View
After creating temporary file, file will be opened in Notepad for viewing or further editing. 

.EXAMPLE
New-TemporaryFile

Creates a new, empty temporary file. Outputs the path to this file. 

.EXAMPLE
$x = get-date | New-TemporaryFile

Creates a new temporary file with the current date and time as its contents.

The path to this file is stored in the variable $x. 

.EXAMPLE
get-date | New-TemporaryFile -Encoding UTF8 -View

Creates a new temporary file with the current date and time as its contents. The contents are written with UTF8 encoding. 

This file is opened in Notepad after it is generated. 

.EXAMPLE
get-date | New-TemporaryFile -Encoding UTF8 -WhatIf

Describes what would happen when executing New-Temporary file, but no action would be taken. No file would be generated. 
#>
}
Top

Comments: