Powershell: Add ability to create a temporary file with a specific extension to New-TemporaryFile

Created on 11 Jul 2017  路  14Comments  路  Source: PowerShell/PowerShell

Related: #3442

Currently, New-TemporaryFile invariably creates temp. files with extension .tmp.

Sometimes, however, it is helpful to create temp. files with a specific filename extension.

Adding an -Extension parameter could provide that:

Desired behavior

> New-TemporaryFile -Extension .ps1

    Directory: C:\Users\jdoe\AppData\Local\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/10/2017   9:53 PM              0 tmpE3B8.ps1

Environment data

PowerShell Core v6.0.0-beta.3
Area-Cmdlets-Utility Issue-Discussion Issue-Enhancement Waiting - DotNetCore

Most helpful comment

Well it's currently on track for .Net 5, meaning we could get it maybe at the end of this year for PS 7.1 if the API arrives in time (the change in PowerShell should be quite small once the API is there)
https://github.com/dotnet/runtime/issues/2048

All 14 comments

What practical scenarios exist when do we need to change the extention?

Whenever you need to create a temporary file to be processed by a utility that requires a specific filename extension.

Using PowerShell itself as an example, say you want to create a throw-away script dynamically, whose path you want to pass to another PowerShell instance via the CLI (powershell -File ...).
Such a throw-away script only works if it has extension *.ps1.

@mklement0 with a recent PR, if you use -File, it doesn't have to have a .ps1 extension :)

@SteveL-MSFT: Good point; I should have used a different example, such as *.csv for files to be processed by Excel, for instance.

I opened the pull request above for it. Please ignore the commits above except for the last one as I squashed the commits in my branch into one commit at the end.

Thanks, @bergmeister!

PR #4612 was closed, waiting for functionality in CoreFX.
Will this cover both extension and total number of files cap?
Where can I vote for the API mentioned in https://github.com/PowerShell/PowerShell/pull/4612#issuecomment-367381354?

@SimonWahlin The corefx issue is here:
https://github.com/dotnet/corefx/issues/24001
The file cap limit is due to the limitation of the Windows API being used (and the short file name) for legacy reasons to avoid breaking changes. One would hope so that the new overload does not make use of that API any more, but please discuss this in the corefx repo/issue.

@SteveL-MSFT Can I recommend a new tag that relates to awaiting upstream features and fixes in CoreFX? The closest we have is Waiting-Netstandard20 which isn't necessarily accurate for proposed but not-yet schedule changes such as the one this PR is pending.

We have sufficient issues waiting on fixes from corefx that I like something more descriptive for this case. I think I'll rename Waiting-Netstandard20 to Waiting-DotNetCore which is more accurate now and still applies to the older issues that have already been marked.

cc @joeyaiello

Yes, please rename.

Label has been renamed.

Given that I just had a situation where this functionality would have helped me, I would say that this is still relevant in in June 2020

Well it's currently on track for .Net 5, meaning we could get it maybe at the end of this year for PS 7.1 if the API arrives in time (the change in PowerShell should be quite small once the API is there)
https://github.com/dotnet/runtime/issues/2048

Was this page helpful?
0 / 5 - 0 ratings