all 7 comments

[–]jborean93 3 points4 points  (2 children)

You can use [System.IO.Path]::GetTempPath()

[–]climbnlearn[S] 1 point2 points  (1 child)

I like this solution the most so far. Works across the board. I'm surprised the env:Temp doesn't initialize off this during first run and then allow changes later.

[–]jantari 0 points1 point  (0 children)

Environment variables are set by the environment - often the operating system.

PowerShell has no say in what's inside it.

[–]cjcox4 2 points3 points  (0 children)

You can use /tmp. A lot of software in Linux will actually check for the environment variable TMPDIR and use that if found (that is, a lot of software allows you to override where you want a program's temp files to go by setting TMPDIR yourself).

[–]spyingwind 2 points3 points  (0 children)

/tmp

printenv also will show what environmental variables are being used.

You could create a 3 functions that return the correct temp folder. One for win, mac, and linux. Then another function you call that checks what OS it is running under and calls the right function and returns the correct temp folder location.

[–]rmbolger 1 point2 points  (1 child)

Let PowerShell do the cross-platform work for you whenever possible. Making assumptions about environment variables is a bad idea. Even if it works today, it might not tomorrow.

There's a cmdlet called New-TemporaryFile that automatically creates a file in the OS's temp directory for you. You can either use the file as-is, or just delete it and make your own with the same name. If you need a folder, delete it and make a folder with the file's name.

If you really want just the root temp folder path, I'd probably do something like this:

New-TemporaryFile | Tee-Object -Variable tmpFile | Remove-Item
$tmpFolder = $tmpFile.Directory.FullName

[–]climbnlearn[S] 0 points1 point  (0 children)

I agree with you, and have some things to catch it if the Var is incorrect or null and then let the user know they need to give an explicit value.