Windows path in a terraform variable sees backslash as an escape character

I am trying to use the following in a Terraform variable to pass through to a azurerm script extension

vm_extensions_script_command = "powershell.exe New-Item 'C:\chris.txt'"

However I get an error that ‘c’ (the first character after the backslash) is an invalid escape character.

I have tried various combinations but can’t seem to get it right?

vm_extensions_script_command = "powershell.exe New-Item 'C:\\chris.txt'"
vm_extensions_script_command = "powershell.exe New-Item C:\\chris.txt"
vm_extensions_script_command = "powershell.exe New-Item ${"C:\chris.txt"}"

The following does work but I’d rather use backslash than forward slash in Windows if I can help it?

vm_extensions_script_command = "powershell.exe New-Item 'C:/chris.txt'}"

Can anyone help?

I would guess that the version using "powershell.exe New-Item C:\\chris.txt" is the correct version, the backslash is correctly escaped and the underlying string will be exatly powershell.exe New-Item C:\chris.txt. Did you get any errors or feedback with that version?

It’s possible that the provider is also trying to interpolate that string in some way, and the workaround would be to use a forward slash to avoid the escaping problems altogether.

With this;

I get the following error;

Thanks for your help so far!

I can’t tell what the context of that error is, but it appears the provider is attempting to use the string literally somehow, but I’m not sure where that would be happening. I would bet that double-escaping the backslash works, but settling on using a forward slash which is allowed specifically for compatibility and escaping issues seems more reasonable.