12

I am trying to download a Google sheet via a batch file. This works:

powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv -OutFile output.tsv"

When I specify which sheet/tab I want by adding &gid=1234, this breaks:

powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234 -OutFile output.tsv"

The error is:

The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.

How do I wrap the ampersand in quotes without breaking the outer quotes for the Command parameter?

1
  • 4
    Did you try to enclose the url in single quotes?
    – Olaf
    Commented Feb 28, 2019 at 1:44

4 Answers 4

18

The URL embedded inside the "..." string passed to powershell -Command must be quoted too, because an unquoted & has special meaning to PowerShell too (though in Windows PowerShell it is currently only reserved for future use; in PowerShell Core it can be used post-positionally to run a command as a background job).

The simplest option is to use embedded '...' quoting, as suggested by Olaf, because ' chars. don't need escaping inside "...". '...' strings in PowerShell are literal strings, which is fine in this case, given that the URL contains no variable references.

powershell -Command "Invoke-WebRequest 'https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234' -OutFile output.tsv"

If embedded "..." quoting is needed for string interpolation, use \" (sic) to escape the embedded (") chars. (note that inside PowerShell, you'd need to use `" or "" instead):

powershell -Command Invoke-WebRequest \"https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234\" -OutFile output.tsv

Note:

  • To avoid problems with cmd.exe's up-front parsing, the outer "..." were omitted above, which still works, because PowerShell simply space-joins multiple arguments before interpreting the result as PowerShell source code.

  • In more complex cases you may prefer the use of outer "...", in which case use of \" for embedded " can situationally break (as it would in this case, due to &), so more elaborate workarounds are needed: "^"" (sic) with powershell.exe, and "" with pwsh.exe, the PowerShell (Core) CLI:

0

I went through the exact same problem recently. If you put a double quote around the URL it won't work. But combination of double quotes and single ones will work:

'""ExampleUrlAddress""'

With this bypass the Powershell should treat the & and other special characters as string.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 22 at 19:43
0

I found a strange differential in the flavours of powershell here.

This works:

powershell.exe -c "Invoke-WebRequest 'https://www.dropbox.com/scl/fo/svwjopvl7rssqifb2wexq/AGfdssrxEr08riq6TBa1AhgSHKQ?rlkey=hiaxu0eo0f9520h92jtofx&st=6fdqwt0k&dl=1' -OutFile %mylocalpath%/data.zip"

and this doesn't:

powershell -Command "Invoke-WebRequest 'https://www.dropbox.com/scl/fo/svwjopvl7rssqifb2wexq/AGfdssrxEr08riq6TBa1AhgSHKQ?rlkey=hiaxu0eo0f9520h92jtofx&st=6fdqwt0k&dl=1' -OutFile %mylocalpath%/data.zip"

-1

My answer: wrap it in double quotation marks like this: "&" so that way,
it’ll be passed as part of a string. If I every try to type something like this:

open & close

it'll say, "Don't forget to put quotation marks around the & symbol." I try again, but this time, like this:

"open & close"

This time, it'll display open and close and not give me that error.

1
  • powershell "open & close" won't work in a .bat file.
    – js2010
    Commented Mar 21 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.