I am experiencing an issue with Powershell's Invoke-Webrequest feature. I know how to use it an all, but some file has a parentheses in it's file name. This is the .ps1 file's code:
Invoke-Webrequest http://FreeZipFiles.com/FreeZipFile.zip -Outfile C:\Users\MyUserName\Desktop\test.zip
Note that FreeZipFiles.com is fictional. When I run the code, I get this error:
FREE! : The term 'FREE!' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:76
... http://FreeZipFiles.com/FreeZipFile (FREE!). ...
~~~
CategoryInfo : ObjectNotFound: (FREE!:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
After looking at this, I realize the parentheses signify "this is a command, run it", but it's not! Anyway, thanks in advance!
CodePudding user response:
You example code shows your URL as http://FreeZipFiles.com/FreeZipFile.zip
but based on your example error message, I suspect you've left out the important part, which is that the URL has a space in it, something like http://FreeZipFiles.com/FreeZipFile.zip (FREE!)
.
In that case, you should ensure that you quote the URL:
Invoke-WebRequest 'http://FreeZipFiles.com/FreeZipFile.zip (FREE!)' -Outfile C:\Users\MyUserName\Desktop\test.zip
The reason it fails the first way is that unquoted values used as arguments are interpreted as strings, but because spaces separate arguments and parameters, you have to take care to ensure spaces are handled correctly. Usually this is done with quoting.
You can use double quotes too, but double quotes can expand certain special characters. For example, if you did this:
Invoke-Webrequest "http://FreeZipFiles.com/$HOST/FreeZipFile.zip" -Outfile C:\Users\MyUserName\Desktop\test.zip
You might be surprised to find that $HOST
will end up taking the value of the $Host
variable in PowerShell, and not be taken literally. Single quotes don't interpret variables that way.
You could technically have escaped the space with a backtick `
which is the escape character in PowerShell, but then you'd also have to escape the parentheses themselves:
Invoke-Webrequest http://FreeZipFiles.com/FreeZipFile.zip` `(FREE!`) -Outfile C:\Users\MyUserName\Desktop\test.zip
It should be obvious that that's not ideal, I'm adding it more as a curiosity.