You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Invoke-WebRequest -OutFile "" strange behaviour on windows #19043
Invoke-WebRequest "https://example.com" -Method GET -OutFile ''
--> No error, no outfile
Invoke-WebRequest "https://example.com" -Method GET -OutFile ' '
--> Error
Same behaviour in PS5 and PS7
Expected behavior
Both or none should throw an error
Actual behavior
The two commands have different behaviours
Error details
No response
Environment data
Name Value
---------
PSVersion 7.3.2
PSEdition Core
GitCommitId 7.3.2
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
It looks like -OutFile $null and -0utFile '' (empty string) are unexpectedly treated as if -OutFile hadn't been specified.
Note that -OutFile ' ' actually succeeds on Unix-like platforms, where you can create a file whose name is a single space.
While it makes sense to report an error on Windows, the error message is misleading: the space is in effect ignored, so that the current directory is targeted, and instead of complaining that you cannot save to a directory, it spuriously claims "Could not find part of a path"
Not surprisingly, Invoke-RestMethod is equally affected.
changed the title [-]Invoke-WebRequest -OutFile "" strange behaviour[/-][+]Invoke-WebRequest -OutFile "" strange behaviour on windows[/+]on Jan 27, 2023
I guess we should have different behaviours for windows and unix
It depends on the capabilities of the file system. The error is most likely due to the impossibility of creating a file with that name. The same thing happens in Linux if you specify an invalid filename.
Indeed - no platform-specific behavior needed. It comes down to this:
That -OutputFile $null / -OutputFile '' cause -OutputFile to be ignored is a bug: an error should be reported that these arguments aren't accepted, just like when you try to pass '' or $null as the file path to Set-Content, for instance.
Separately, there's a mostly cosmetic problem when an invalid path is passed (and what constitutes an invalid path is platform-specific, with very few constraints existing on Unix-like platforms): the currently misleading error message should be fixed.
Activity
mklement0 commentedon Jan 27, 2023
It looks like
-OutFile $nulland-0utFile ''(empty string) are unexpectedly treated as if-OutFilehadn't been specified.Note that
-OutFile ' 'actually succeeds on Unix-like platforms, where you can create a file whose name is a single space.While it makes sense to report an error on Windows, the error message is misleading: the space is in effect ignored, so that the current directory is targeted, and instead of complaining that you cannot save to a directory, it spuriously claims "Could not find part of a path"
Not surprisingly,
Invoke-RestMethodis equally affected.237dmitry commentedon Jan 27, 2023
There are no errors if the correct file name is specified. If the argument to this parameter is a zero-length string, then the parameter is ignored.
[-]Invoke-WebRequest -OutFile "" strange behaviour[/-][+]Invoke-WebRequest -OutFile "" strange behaviour on windows[/+]CarloToso commentedon Jan 27, 2023
So, I guess we should have different behaviours for windows and unix
237dmitry commentedon Jan 27, 2023
It depends on the capabilities of the file system. The error is most likely due to the impossibility of creating a file with that name. The same thing happens in Linux if you specify an invalid filename.
mklement0 commentedon Jan 27, 2023
Indeed - no platform-specific behavior needed. It comes down to this:
That
-OutputFile $null/-OutputFile ''cause-OutputFileto be ignored is a bug: an error should be reported that these arguments aren't accepted, just like when you try to pass''or$nullas the file path toSet-Content, for instance.Separately, there's a mostly cosmetic problem when an invalid path is passed (and what constitutes an invalid path is platform-specific, with very few constraints existing on Unix-like platforms): the currently misleading error message should be fixed.