Skip to content

Go-Temp

Sets the working directory to the temporary folder

#requires -version 5.1

function GetTempDir {
        if ("$env:TEMP" -ne "") { return "$env:TEMP" }
        if ("$env:TMP" -ne "")  { return "$env:TMP" }
	if ($IsLinux -or $IsMacOS) { return "/tmp" }
        return "C:\Temp"
}

try {
	$path = GetTempDir
	if (-not(Test-Path "$path" -pathType container)) { throw "The temporary folder at '$path' doesn't exist (yet)" }
	Set-Location "$path"
	$files = Get-ChildItem $path -attributes !Directory
	$folders = Get-ChildItem $path -attributes Directory
	"??$path entered (has $($files.Count) files and $($folders.Count) folders)"
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0])"
	exit 1
}

This script has no configurable parameters.

An interactive directory of PowerShell scripts.