SOLVED

Not able to output text to a file in powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-1680229%22%20slang%3D%22en-US%22%3ENot%20able%20to%20output%20text%20to%20a%20file%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1680229%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3EIs%20there%20a%20way%20I%20can%20add%20a%20variable%20with%20inverted%20commas%20along%20with%20text%20to%20a%20file%20using%20powershell%3F%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3Efor%20example%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3E%24name%20%3D%20Read-host%20-prompt%20%22Enter%20your%20name%22%20%3C%2FSPAN%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3Eecho%20%7BYour%20name%20is%20%22%24name%22%7D%20%26gt%3Bname.txt%20%3C%2FSPAN%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3Epause%20%3C%2FSPAN%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22css-901oao%20css-16my406%20r-1qd0xha%20r-ad9z0x%20r-bcqeeo%20r-qvutc0%22%3EWhen%20I%20run%20it%20the%20variable%20no%20longer%20has%20its%20value%2C%20is%20there%20a%20solution%20for%20this%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1680229%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1681016%22%20slang%3D%22en-US%22%3ERe%3A%20Not%20able%20to%20output%20text%20to%20a%20file%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1681016%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717814%22%20target%3D%22_blank%22%3E%40Waqaarahsab%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERemove%20the%20echo%3C%2FP%3E%3CP%3E%24name%20%3D%20Read-host%20-prompt%20%22Enter%20your%20name%22%3C%2FP%3E%3CP%3E%22Your%20name%20is%20%24(%24name)%22%20%26gt%3Bname.txt%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1685140%22%20slang%3D%22en-US%22%3ERe%3A%20Not%20able%20to%20output%20text%20to%20a%20file%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1685140%22%20slang%3D%22en-US%22%3EIs%20there%20a%20way%20to%20add%20inverted%20comma%20before%20the%20variable%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1686907%22%20slang%3D%22en-US%22%3ERe%3A%20Not%20able%20to%20output%20text%20to%20a%20file%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1686907%22%20slang%3D%22en-US%22%3E%22Your%20name%20is%20%60%22%24(%24name)%60%22%22%20%26gt%3Bname.txt%3CBR%20%2F%3EYou%20will%20need%20to%20escape%20the%20inverted%20comma%2C%3CBR%20%2F%3Echeck%20about%20the%20escape%20character%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fadamtheautomator.com%2Fpowershell-escape-double-quotes%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fadamtheautomator.com%2Fpowershell-escape-double-quotes%2F%3C%2FA%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional 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
Highlighted

@Waqaarahsab 

Remove the echo

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

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

Highlighted
Is there a way to add inverted comma before the variable?
Highlighted
Best Response confirmed by Waqaarahsab (Occasional 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/
Highlighted
Thank you so much! I was looking for this everywhere.