PowerShell/Scripts/cd-temp.ps1

32 lines
809 B
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Sets the working directory to the temporary folder
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script changes the working directory to the temporary folder.
.EXAMPLE
PS> ./cd-temp
2022-05-04 11:50:18 +02:00
📂C:\Users\Markus\AppData\Local\Temp
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-05-04 11:50:18 +02:00
Author: Markus Fleschutz | License: CC0
#>
2021-12-09 16:36:44 +01:00
function GetTempDir {
2022-02-25 13:11:33 +01:00
if ("$env:TEMP" -ne "") { return "$env:TEMP" }
if ("$env:TMP" -ne "") { return "$env:TMP" }
if ($IsLinux) { return "/tmp" }
return "C:\Temp"
2021-12-09 16:36:44 +01:00
}
try {
2022-02-25 13:11:33 +01:00
$Path = GetTempDir
2023-08-06 21:35:36 +02:00
if (-not(Test-Path "$Path" -pathType container)) { throw "Temporary folder at 📂$Path doesn't exist (yet)" }
2022-02-25 13:11:33 +01:00
Set-Location "$Path"
"📂$Path"
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}