1

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.

1 Answer 1

2

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)" | ...
Sign up to request clarification or add additional context in comments.

1 Comment

Seems to work! However, instead of the value for cowNumber$($i+1) in my text file, I'm getting System.Management.Automation.PSVariable... any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.