PowerShell Temporary Work
In this issue:
When you think about it, a lot of the work we do in PowerShell is temporary. We run a command. Maybe we save the output to a variable. We define functions and map PSDrives. When PowerShell ends, those things go away. They are temporary. However, there may be other things we create such as temporary files and directories, that is, things that can be defined outside of PowerShell that could persist. I wanted to spend a little time exploring this and give you some tips on how to manage these temporary things.
Temporary Locations
First up, where are temporary things stored? The answer is, it depends. On Windows, there should be an environment variable, %TEMP%
. You most likely also have a %TMP%
variable. These should point to the same location. You can check this in PowerShell by looking at the $env:
drive which exposes environment variables as items.
PS C:\> dir env:t*
Name Value
---- -----
TMP C:\Users\Jeff\AppData\Local\Temp
TEMP C:\Users\Jeff\AppData\Local\Temp
You can easily reference the location like this:
PS C:\> dir $env:TEMP -file -Recurse | Measure length -sum -AllStats
Count : 1365
Average : 421955.55970696
Sum : 575969339
Maximum : 360541325
Minimum : 0
StandardDeviation : 10218686.0267263
Property : Length
In Windows, there are also %TEMP%
and %TMP%
environment variables for the machine. These are not captured in the $env:
drive. You can access these using the .NET method [System.Environment]::GetEnvironmentVariable()
. The second parameter specifies the scope, which can be Process
, User
, or Machine
. The Process
scope is what you see in $env:
. The User
scope is specific to your user account, and the Machine
scope is for all users on the machine.
PS C:\> [System.Environment]::GetEnvironmentVariable("temp","user")
C:\Users\Jeff\AppData\Local\Temp
PS C:\> [System.Environment]::GetEnvironmentVariable("temp","process")
C:\Users\Jeff\AppData\Local\Temp
PS C:\> [System.Environment]::GetEnvironmentVariable("temp","machine")
C:\WINDOWS\TEMP
PS C:\> [System.Environment]::GetEnvironmentVariable("tmp","machine")
C:\WINDOWS\TEMP
Non-Windows
In PowerShell 7 on non-Windows platforms, there is no env:TEMP
or env:TMP
variable. However, you can still access the temporary location using the .NET method:
PS /home/jeff> Test-Path env:\HOME
True
PS /home/jeff> Test-Path env:\TEMP
False
PS /home/jeff> Test-Path env:\TMP
False
> Items in the env:
drive are case-insensitive on Windows but case-sensitive on non-Windows platforms.
Fortunately, we can use the .NET Framework to get the temporary location:
PS /home/jeff> [System.IO.Path]::GetTempPath()
/tmp/
This will also work on Windows.
PS C:\> [System.IO.Path]::GetTempPath()
C:\Users\Jeff\AppData\Local\Temp\
If you are writing cross-platform code, you might want to use this method to get the temporary location.
An alternative on non-Windows platforms, you could define the missing environment variable:
New-Item -Path env: -Name TEMP -Value ([System.IO.Path]::GetTempPath()) -Force
This will create or update the TEMP
environment variable in the current session.
PS /home/jeff> Get-Item env:TEMP
Name Value
---- -----
TEMP /tmp/
Don't forget the name is case-sensitive on non-Windows platforms.
Temporary Files
A typical temporary use case is to create and use a temporary file. This is something you don't need to persist and don't care if it is eventually deleted. Ideally, temporary files should be created in the temporary location defined in %TEMP%
or %TMP%
.
New-TemporaryFile
This is easily accomplished using a cmdlet you may not have known exists called New-TemporaryFile
. This cmdlet creates a zero-length temporary file in the temporary location and returns a FileInfo
object representing the file.
PS C:\> New-TemporaryFile | tee -Variable tmpFile
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/16/2025 1:53 PM 0 tmppleag4.tmp
PS C:\> Get-Date | Out-File $tmpFile
PS C:\> Get-Item $tmpFile
Directory: C:\Users\Jeff\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/16/2025 1:53 PM 43 tmppleag4.tmp