2

I want to echo some text that contains double quotation marks in a text file using PowerShell:

powershell echo " > file.txt

But this fails, I did my searching, and it suggested to use the ` character to escape quotes, but this did not work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
suntop
  • 21
  • 1

1 Answers1

0

You want to do do something like this as per the PowerShell quoting rules:

Use single quote to define your string:

echo '"' > file.txt

Or use double quotes to define your string and escape your character with the ` character:

echo "`"" > file.txt

You could also use the Set-Content PowerShell command to create the file as well:

'"' | Set-Content file.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane Powell
  • 13,698
  • 2
  • 49
  • 61
  • this (echo '"' > file.txt) and this (echo "`"" > file.txt) only echos to the command prompt and this ('"' | set-content file.txt) generates an empty text file only – suntop Jan 28 '18 at 20:23
  • Not sure what you are doing, but it works fine for me and in a get a double quote only in the file.txt file. Are you sure you are running the commands in a powershell prompt? – Shane Powell Jan 28 '18 at 21:06
  • actually I need to run the command from a windows command prompt not from a powershell prompt, maybe that is the problem, how do I run this command from a powershell prompt ? – suntop Jan 29 '18 at 06:11
  • Run powershell from start menu or from in cod.exe should get you a powershell prompt. Cmd.exe has weird rules around escaping quote chars, I’m not sure it can be done with the echo command. – Shane Powell Jan 29 '18 at 06:49
  • it can be done using the echo command like so (echo ^" > file.txt) - however when I prefix the previous command with powershell it does not work – suntop Jan 29 '18 at 09:47