0

Code:

$text=Get-Content -Path "E:\1.txt"
$text.GetType() | Format-Table -AutoSize
For($i=0; $i -le 5 ;$i++)
{
    $var=Write-host '$'text[$i] 
    $var
}

Actual Result:

$ text[0]
$ text[1]
$ text[2]
$ text[3]
$ text[4]
$ text[5]

I need below Result:

$text[0]
$text[1]
$text[2]
$text[3]
$text[4]
$text[5]
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 3
    It's pretty straight forward: `\`$text[$i]` – Clijsters Apr 22 '21 at 11:42
  • 2
    if it is powershell only, why did you tag c#? – Franz Gleichmann Apr 22 '21 at 11:43
  • As @Clijsters says. Simply do ``Write-Host "`$text[$i]"`` – Theo Apr 22 '21 at 12:45
  • Are you looking to display literally just `$text[$i]`? Or the the value of it? You can always single quote to display the literal variable, you can also escape the variable depending on what you want to do. You can also use a sub-expression `$()` to do something as well such as displaying the value in the variable. – Abraham Zinala Apr 22 '21 at 12:53

2 Answers2

2

If you must use the quotes, using -separator also works:

$text=Get-Content -Path "E:\1.txt"
$text.GetType() | Format-Table -AutoSize

For($i=0; $i -le 5 ;$i++)
{
    $var=Write-host '$'text[$i] -Separator ''
    $var
}
zett42
  • 25,437
  • 3
  • 35
  • 72
  • 1
    While that is an interesting workaround, note that the fundamental problem is that `Write-Host` does't write to PowerShell's success output stream and prints directly to the host (in PSv5+, strictly speaking, via the _information_ output stream, number `6`). Therefore, nothing is captured in variable `$var`. – mklement0 Apr 22 '21 at 13:19
1

Your code fundamentally doesn't do what you intend it to do, due to the mistaken use of Write-Host:

# !! Doesn't capture anything in $var, prints directly to the screen.
$var=Write-host '$'text[$i] 
$var # !! is effectively $null and produces no output.

See the bottom section for details.


Instead, what you want is an expandable string (aka interpolating string, "..."-enclosed), with selective `-escaping of the $ character you want to be treated verbatim:

$var= "`$text[$i]" # Expandable string; ` escapes the $ char., $i is expanded
$var

There are other ways to construct the desired string:

  • $var = '$text[{0}]' -f $i, using -f, the format operator.
  • $var = '$' + "text[$i]", using string concatenation with +

but the above approach is simplest in your case.


As for what you tried:

  • Write-Host is typically - and definitely in your case - the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g., $value instead of Write-Host $value (or use Write-Output $value, though that is rarely needed); see this answer.

  • What you thought of as a single argument, '$'text[$i], was actually passed as two arguments, verbatim $ and expanded text[$i] (e.g., text[0]) and because Write-Host simply space-concatenates multiple arguments, a space was effectively inserted in the (for-display) output.

  • That '$'text[$i] becomes two arguments is a perhaps surprising PowerShell idiosyncrasy; unlike in POSIX-compatible shells such as bash, composing a single string argument from a mix of unquoted and (potentially differently) quoted parts only works if the argument starts with an unquoted substring or (mere) variable reference; for instance:

    • Write-Output foo'bar none' does pass a single argument (passes foobar none), whereas
    • Write-Output 'bar none'foo does not (passes bar none and foo)
    • See this answer for more information.
mklement0
  • 382,024
  • 64
  • 607
  • 775