Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upProvide a platform-agnostic way to determine the temp. directory, e.g., via an automatic variable. #4216
Comments
This comment has been minimized.
This comment has been minimized.
lukeb1961
commented
Jul 11, 2017
|
what about something like: (New-TemporaryFile).DirectoryName |
This comment has been minimized.
This comment has been minimized.
|
@lukeb1961: That indeed yields the platform-specific temporary directory, but it has an unwanted side effect: In other words: every time you execute Additionally, it's a computationally expensive approach. |
This comment has been minimized.
This comment has been minimized.
|
We've had discussed this in a nutshell. Thank you for opening the Issue. Internally we already have "TemporaryDirectory", just "GetTemporaryDirectory()" and "CreateTemporaryDirectory()". We could make it public. |
iSazonov
added
Area-Cmdlets-Utility
Issue-Discussion
labels
Jul 11, 2017
SteveL-MSFT
referenced this issue
Jul 14, 2017
Closed
OS environment variable mappings should be better specified/implemented #1262
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Jul 14, 2017
|
How about going with $Env:TMP on all platforms? That is not quite like Windows, Mac, or Linux. No turf war. |
This comment has been minimized.
This comment has been minimized.
|
@Liturgist: An automatic PowerShell variable such as |
This comment has been minimized.
This comment has been minimized.
|
If we went the variable route, it would have to be something like $interop:TEMP to avoid name collision. A variable is probably easier to use in scripts than an API although we can certainly support both. In #3571 I suggested other variables in addition to TEMP |
This comment has been minimized.
This comment has been minimized.
|
Good point; an If we take a step back: If we want PowerShell to become the lingua franca of the shell world, do we really want to frame things as "interop"? How about trusting PowerShell's automatic variables to Do the Right Thing(TM) generically, which would call for a namespace such as |
This comment has been minimized.
This comment has been minimized.
|
@mklement0 the Personally, I prefer |
This comment has been minimized.
This comment has been minimized.
|
I'm glad to hear it, @SteveL-MSFT. On a related note, I think a separate namespace for all preference variables would be beneficial as well, such as The caveat is that even though collisions are far less likely than with unqualified variable names, someone could have done something like the following: # Define custom drive 'ps:'
New-PSDrive 'ps' FileSystem '/tmp'; Get-ChildItem ps: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
That works, but it is both hard to remember and cumbersome to type. |
This comment has been minimized.
This comment has been minimized.
|
If we have |
This comment has been minimized.
This comment has been minimized.
|
Since the term path is ambiguous (even though it is used in the .NET API to refer to a directory path in this case), my preference would be That said, a cmdlet is a bit heavy-handed for something that returns a single piece of information with no variations in functionality. Consider the existing |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
We would make |
This comment has been minimized.
This comment has been minimized.
dragonwolf83
commented
Jul 17, 2017
|
I would prefer a consistent way of mapping environment variables that are basically the same just expressed differently. Are we saying we will map all path variables to drives, like Home:? |
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Jul 17, 2017
|
@mklement0 - I am not quite sure why |
This comment has been minimized.
This comment has been minimized.
dragonwolf83
commented
Jul 17, 2017
|
@Liturgist I think the notion is that anything in the The proposals for a separate namespace or drive eliminate that issue as it allows for PowerShell to have it's own commonly defined variables that can be used cross-platform and not block access to the real environment variables. Another idea though for a namespace could be |
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Jul 18, 2017
|
@dragonwolf83 - Would To go with |
This comment has been minimized.
This comment has been minimized.
dragonwolf83
commented
Jul 18, 2017
|
No, I don't think the PowerShell team would move
In other words, it exists exactly where it belongs. Take a look at the screenshot below. It shows the Environment Variables defined on my Windows system. It has TEMP and TMP as environment variables for my user Temp path. It also has PSModulePath. So all of those other variables, I would expect to see in |
This comment has been minimized.
This comment has been minimized.
|
To add to @dragonwolf83's helpful comments and to respond to your comment addressed to me:
My perhaps glaringly-obvious-didn't-need-to-be-said point was that in the absence of PowerShell actually defining such an environment variable (which, as stated, would be ill-advised - also see below), PowerShell shouldn't pretend that it exists. PowerShell respects / defines only two environment variables, according to
These variables are environment variables for a good reason: they allow you to control PowerShell's startup behavior externally. By contrast, there is NO good reason to define the directory for temporary files and directories as an environment variable:
If a reader is familiar primarily with I definitely like the idea of multiple namespaces for PowerShell's automatic variables (such as the suggested |
This comment has been minimized.
This comment has been minimized.
|
Seems like there's been enough good discussion that perhaps this should be a RFC? |
This comment has been minimized.
This comment has been minimized.
In [io.path]::GetTempPath() the [io.path] is a namespace - should we introduce a Powershell "native" namespaces like |
This comment has been minimized.
This comment has been minimized.
|
Before we tackle an RFC, let me try to summarize what I perceive to be the use cases and perhaps get a better understanding of the scope of such an RFC:
As for the introduction of PowerShell-controlled namespaces: Note that the current namespace notation - e.g., If we now introduce namespaces such as So what should an RFC cover?
I could tackle the former, but I'm not sure I can come up with a sensible approach to the latter (and it may not be worth doing). |
This comment has been minimized.
This comment has been minimized.
|
@mklement0 thanks for summarizing all the different related discussions. I think just scoping the |
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Jul 19, 2017
|
@mklement0 - Very nice and helpful summary. I would much rather have |
This comment has been minimized.
This comment has been minimized.
|
Thank you, @Liturgist. I hear you re On the opposite end of the spectrum, you could argue (as someone already has, but I forget where) that there should be just one generic Either way, I suggest you make your voice heard at #3442. @SteveL-MSFT: Thank you for the guidance, but despite my initial willingness to tackle this RFC, I'd like to bow out of this [self-]assignment. |
This comment has been minimized.
This comment has been minimized.
|
Just a couple comments. This thread has gotten far afield of the original request which is simply to have a platform agnostic method for determining the TEMP folder. Perhaps discussions about creating ephemeral temporary files and folders should be another RFC? Second, here is the code I use for creating the TEMP variable and COMPUTERNAME variables on PowerShell Core. So far all my examples are for making the windows abstractions present on linux because I have a lot of code written for Windows. Also, to know which variables to port across I basically try to judge what is VERY common in my scripts and exists as an entity on the linux side (e.g. hostname, temp), but is simply not exposed in the way my script expects. In my mind the idea behind compatibility would be to facilitate adoption through [a] reuse of existing code, [b] ability to reuse familiar abstractions and [c] ability to write cross platform scripts. I hope that [a] would not get overlooked with an overly greenfields perspective as it is this one that I've used the most.
|
This comment has been minimized.
This comment has been minimized.
|
@DarwinJS: Yes, the thrust of your RFC is quite different, so I agree that what the discussion here drifted toward (despite its initial focus being just one piece of environment information) should be its own RFC. In short:
The two approaches are not mutually exclusive. My only concern with your specific approach is that you're creating environment variables on Unix platforms, which are seen by all child processes, even though the variables are only used PowerShell-internally (to restate a concern expressed in an earlier comment). |
This comment has been minimized.
This comment has been minimized.
|
I'm not sure I understand why it matters if child processes don't recognize environment variables they don't use - it's pretty standard fare for software and scripts to define custom environment variables that child processes - including operating system binaries - will ignore. Overall I think it will help adoption if there is NOT a new data type within PowerShell to do basic things like identify the disk temp location. "Environment Variables" and "Language Variables" seem normal and familiar to both sides of the house. If a namespace is decided on, I would not use "psenv" If you have to turn around and unexplain the universally obvious implication of the string "env" - it seems like a learning anti-pattern. Anytime disambiguation can be incorporated at the initial naming, it saves billions of verbal and written repetitions of the following qualifier:
|
This comment has been minimized.
This comment has been minimized.
|
I realize that your polyfill hinges on defining environment variables, and given that neither More generally, the concern is not about child processes ignoring environment variables, but about naming conflicts: different utilities using an environment variable of the same name for different purposes, unbeknownst to each other. The namespace of environment variables is shared and not constrained in any way (except syntactically), and the only way to avoid collisions is by adopting naming conventions - a prefix such as Therefore, the more robust approach is to avoid use of environment variables altogether, if they're not strictly needed - which is the case here (each session can derive the values from the existing environment). As for introducing what you call a new data type: Familiarity is great and helps those familiar with one old way of doing things, but it's not a great foundation for a shell aspiring to be inherently multi-platform - and especially a shell whose success is owed to doing things in a new, superior way. To write multi-platform scripts, users need abstractions they can rely on, and, conversely, not using those abstractions will inform them that they're venturing into platform-specific territory (which may be perfectly fine, depending on the use case). As for the namespace |
This comment has been minimized.
This comment has been minimized.
|
Good points - I'm sure TEMP is ripe for conflict - so by being an optional "Win to Linux" compat module, script testing would hopefully reveal that. Familiarity is not the only value is so called "old" ways - they also have the most ubiquitous implementation. And "new" ways can be over engineered - just last week I helped a colleague replace 13 lines of code they received from a large company with one line. The new code used the exceptionally over-complex and incompletely implemented "*ScheduledJob" CMDLets. If new implementations get overly focused on "design purity" they can be a step backwards. I'm not saying that is the case with proposing a new namespace - just that if "adoptability" is not forefront for a new implementation, it can end up being shelfware. Another consideration with new name space is that if it is not back ported to PSH 5, then it will not be ubiquitous for quite some time. I'll still need to "figure out" if I can rely on the namespace for the code I write that needs to be PSH 5 / PSH 6 + multiplatform. Then I will need to custom code someting for PSH 5. Actually, Windows PSH 5 is probably the dominant use case for the foreseeable future. But maybe this would just guide the implementation toward a module so that those that wish can run it on versions older than 6? |
iSazonov
referenced this issue
Jul 31, 2017
Open
Improving the discoverability of PowerShell variables #4394
This comment has been minimized.
This comment has been minimized.
|
Our discussion splitted on three main directions:
@mklement0 Could you please open new Issue(s) (for 1. and 2. ) to summary the disscusion and close the Issue? |
This comment has been minimized.
This comment has been minimized.
|
Re 1.: This is covered by @DarwinJS's existing RFC.
I hear you, but keeping track of which feature was introduced when is an unavoidable challenge if you want to innovate. I suggest we continue the conversation at #4394. |
mklement0
closed this
Aug 12, 2017
mklement0
referenced this issue
May 8, 2018
Closed
Should we do path normalizations on Unix well as on Windows? #6833
mklement0
referenced this issue
May 31, 2018
Open
Add support for special folders (known folders) via a separate provider / namespace #6966
PlagueHO
referenced this issue
Sep 3, 2018
Closed
Shadow-Copy Function Throws Exception when run on PS Core on Linux or OSX #113
This comment has been minimized.
This comment has been minimized.
swngdnz
commented
Oct 6, 2018
|
$env:tmp or $env:temp is the predominate use-case today. promoting this to other platforms is trivial. whether you consider it interop or legacy is irrelevant. it supports the predominance of all existing scripts. |
This comment has been minimized.
This comment has been minimized.
Jaykul
commented
Oct 6, 2018
Short answer:
|
This comment has been minimized.
This comment has been minimized.
LawrenceHwang
commented
Oct 6, 2018
•
|
While I like the simplicity and consistency (a lot) with As a PowerShell user, I have always appreciated in PowerShell being closer to meaningful and plain English. By NOT providing that option and implying people should learn .NET is going to erode people's trust. |
SteveL-MSFT
reopened this
Oct 6, 2018
This comment has been minimized.
This comment has been minimized.
swngdnz
commented
Oct 6, 2018
|
Requiring an admin-scripter to use [IO.Path]::GetTempPath() is obscene. It's not discoverable, it's not obvious, and it's not powershell. I personally believe that promoting $env:tmp is the right answer, but someone could argue it's also not powershell. After some personal consideration, I think a new cmdlet Get-TemporaryDirectory (or a similar name) is the proper answer. I don't understand "New-TempDirectory". The normal use-case is not creating a directory (which New-* indicates) - it's retrieving the name of an existing directory (which Get-* indicates). I'm "meh" about a temp: drive because, for it to be discoverable, it also requires one or more cmdlets. Instead of offering more "freedom" to the scripter, a temp: drive feels constraining. |
This comment has been minimized.
This comment has been minimized.
|
Another look to be completely PowerShell native. Can we avoid the explicit use of temporary directories? # Create the file in temporary directory
$temp= New-Item -Path xyz.txt -Temporary
# Get the file and copy it to temporary directory
$var2 = Get-Item -Path sample.txt; $tempVar2 = Copy-Item $var2 -Temporary
# The temporary directory is automatically created and dropped for the scope |
This comment has been minimized.
This comment has been minimized.
|
I feel it should simply abstract away the existing platform with the longest standing powershell platform's references being preferred so that the long standing code can be more easily reused. Like this:
|
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Oct 6, 2018
|
Looking back at the subject of this thread, some may want to get what might be considered the traditional "tmp" directory. This could return [System.IO.Path]::GetTempPath(). This would obviate the need for a new namespace or provider (ps:, PSEnv:, etc.).
If a developer wants to use $Env:TEMP, or $Env:TMPDIR, or /tmp then they are free to do that. They would be making their code platform dependent. Applications need mechanism to create unique directories and files in which to store things. What if we extended New-Item to be able to create one or more temporary directories and files. It would return a list of the item names. The item names returned will already be created and ready for use.
|
This comment has been minimized.
This comment has been minimized.
|
Just to summarize the discussion so far: We're talking about two complementary use cases here, although one is far more important:
|
This comment has been minimized.
This comment has been minimized.
|
@mklement0 Thanks for the great summarization! I think we could continue considering scenarios in which all these options could be useful (like "get a file from web/network, put in temp, process the file (unpack/decoding...), remove the temp"). I think we can do more wonderful things than just return temp directory. |
This comment has been minimized.
This comment has been minimized.
|
I hit this recently while playing with Azure Functions. I'm so used to having |
This comment has been minimized.
This comment has been minimized.
|
Based on above discussion the simple mapping to Temp: drive looks not so useful and not resolve common scenarios. (Users can implement this with one command in profile. In Azure it is possible simply cd to temp.) |
This comment has been minimized.
This comment has been minimized.
|
At that level it's not especially useful, @iSazonov, but the change is helpful to future module authors, who could simply refer to |
This comment has been minimized.
This comment has been minimized.
|
@SteveL-MSFT - maybe this issue should be renamed and re-scoped to "implementation-agnostic" to cover things like cloud shells without implying they are a platform in the same way an OS is. The name change helps reflect the broader value proposition when bringing serverless / non-OS implementions into scope. |
This comment has been minimized.
This comment has been minimized.
Liturgist
commented
Jan 19, 2019
|
Thank you for curating the issues, Michael Klement. It seems that the tradition of a tmp/temp directory is that every program can us it in whatever way it wants. That works until name collision or there are multiple instances of the same program. In the first case, the applications must choose different names. In the second, the application may identify its files by including unique information such as its PID in the filename. (a) I fully agree that with guaranteed uniqueness, the application should not care about where a temporary file or directory is located or the name. A constraint should be that it is possible to locate post-mortem the temporary files and directories to enable anomaly remediation (finding and fixing bugs). Does this suggest that automatic temp cleanup should be a preference setting? The suggestion of -NamePrefix was based on an application being able to identify its own temprrary files in a single pool. Although guaranteed uniqueness obviates this as a requirement, the developer may find comfort in recognizing foo_xxxx.tmp, bar_xxxx.tmp, bosh_xxxx.tmp, and others when mutiple temporary files are in use. I agree that if we have New-TemporaryFile that we should have New-TemporaryDirectory. |
This comment has been minimized.
This comment has been minimized.
|
Certainly |

mklement0 commentedJul 11, 2017
Related: #3442 and #4215
The various supported platforms have different native ways to determine the directory in which to store temporary files and directories:
$env:TEMP$env:TMPDIR/tmpCurrently, there is no platform-agnostic way to refer to this directory, which is cumbersome.
An automatic variable, say
$TEMP, could provide this abstraction.Environment data