New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set-Content should not allow writing to multiple files using wildcards #6729
Comments
Assuming they knew about it, I easily can see people using this to (re)initialize a set of file so I don't think it's really a bucket 3 change. Consider that
is certainly much pithier than
|
Yes, as with any breaking change that takes away functionality there is a risk that people have relied on it. My guess is that, given that the behavior is (a) surprising to begin with and (b) is not present in As for the pithiness; not quite as concise as the original, but a reasonable substitute, in my opinion, given the benefits of taking away the risky behavior: 'hi' | Set-Content (Get-Item *.txt) Note that, due to #6057, |
Based on
your issue is not what I thought it was. It appears that you still want |
Yes, if you pass an array of literal file paths to No change need there, because these parameters are already typed By contrast, the wildcard-based way of targeting multiple file is the fraught behavior, which I think should be eliminated. I used the Now you could ask what should happen if an array is passed to For simplicity, my vote is for disallowing any wildcard expression that matches more than 1 file, whether stand-alone or as part of an array. To summarize, the desired behavior is:
|
This seems like the sort of thing that should be the default, but able to be worked around. You've provided one workaround, but consider perhaps that we could also allow the # This doesn't work:
"blank" | Set-Content -Path "*.txt"
# But this does
"blank" | Set-Content -Path "*.txt" -Force |
Having the ability be opt-in is definitely preferable, but note that we'd be adding meaning to the existing use of Also, you could argue that we'd have to implement the same thing for all output-to-file cmdlets ( On the other hand, as I've just realized, |
I think we could remove the dangerous behavior. In File System Provider V2 at least. And follow to one principle for all cmdlet - target path parameter should always be LiteralPath. (We already did this for web cmdlets and Out-File is under question too #9475.) |
@PowerShell/powershell-committee looked at this and we compared it to behavior in |
This comment has been minimized.
This comment has been minimized.
This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes. |
…rror. Error message: Out-File: Cannot perform operation because the wildcard path D:\a\grammars-v4\grammars-v4\molecule\examples\(NH4)2[Pt(SCN)6].txt.tree.out did not resolve to a file. Crazily, this is intentional, despite the fact that Out-File (and it's synonym, ">") can only generate a single file. See ttps://github.com/PowerShell/PowerShell/issues/6729#issuecomment-984165303.
Follow-up from #6714
Currently,
Set-Content
writes to whatever existing files match a wildcard pattern passed to its (implied)-Path
parameter.For instance, the following will overwrite all existing
*.txt
files in the current directory:This behavior is risky and should be changed to match the more sensible behavior of other output-to-file cmdlets such as
Out-File
(and therefore the>
/>>
operators), which generate an error if the wildcard expression resolves to more than one file.It is worth examining all output-to-file cmdlets on this occasion.
Technically, this is a breaking change, but presumably one that falls into Bucket 3: Unlikely Grey Area.
Environment data
The text was updated successfully, but these errors were encountered: