Sheeboid
Sheeboid

Reputation: 35

Powershell - Write-output to text syntax for variables and strings

I have the following:

$i = a number that changes

new-variable -name cowNumber$($i+1) -value [some value]

I want to output this to a text file so that it reads:

Write-Output "The number of cows in field2: [cowNumber value]" | Out-file [some path] -append

where field2 is formatted as field($i+1) and [cowNumber value] is cowNumber$($i+1)

Any information is appreciated, thanks.

Upvotes: 1

Views: 10065

Answers (1)

Keith Hill
Keith Hill

Reputation: 201602

You need to use Get-Variable to get a dynamically named variable e.g.:

Write-Output "The number of cows in field$($i+1): $(Get-Variable cowNumber$($i+1) -ValueOnly)" | ...

Upvotes: 2

Related Questions