SOLVED

Not able to output text to a file in powershell

Copper Contributor

Is there a way I can add a variable with inverted commas along with text to a file using powershell?

for example:

$name = Read-host -prompt "Enter your name"

echo {Your name is "$name"} >name.txt

pause

When I run it the variable no longer has its value, is there a solution for this

4 Replies

@Waqaarahsab 

Remove the echo

$name = Read-host -prompt "Enter your name"

"Your name is $($name)" >name.txt

Is there a way to add inverted comma before the variable?
best response confirmed by Waqaarahsab (Copper Contributor)
Solution
"Your name is `"$($name)`"" >name.txt
You will need to escape the inverted comma,
check about the escape character
https://adamtheautomator.com/powershell-escape-double-quotes/
Thank you so much! I was looking for this everywhere.
1 best response

Accepted Solutions
best response confirmed by Waqaarahsab (Copper Contributor)
Solution
"Your name is `"$($name)`"" >name.txt
You will need to escape the inverted comma,
check about the escape character
https://adamtheautomator.com/powershell-escape-double-quotes/

View solution in original post