0

This may be as noob question but how do i get a variable into quotes

Write-host 
$string1 = Read-Host

Write-host $string1

If i typed abc for write it will show abc.

how to i get it to show "abc"

doing "$string1" would just show as "$string" and not the value of it in quotes

mklement0
  • 382,024
  • 64
  • 607
  • 775
Kidbuu
  • 59
  • 7

1 Answers1

1

The enclosing " characters in "$string1" have syntactic function and are therefore stripped during parsing.

To make such characters a part of the data, you must embed escaped versions of them (`"):

PS> "`"$string1`"" # assume that $string1 contains verbatim: foo
"foo"

` is PowerShell's general escape character; in the context of "..." strings (expandable strings) you can use `" and "" interchangeably.

Also note that the command above relies on PowerShell's implicit output feature - no need for Write-Host, unless you explicitly want to write to the display only, without PowerShell's rich output formatting - see this answer for more information.

mklement0
  • 382,024
  • 64
  • 607
  • 775