Reputation: 23
I'm writing a script that creates new variables as needed and I have them working fine. What I need now is to return the length of the those variables.
This of course returns the value of '6'
$strText = "Yellow"
$strText.length
But here's where I'm having trouble. This returns '1.length'
$bar = 1
New-Variable "foo$bar" "hello"
write-host $foo$bar.length
and this returns 'foo1.length'
$bar = 1
New-Variable "foo$bar" "hello"
write-host foo$bar.length
I'm obviously looking for '5' in this case.
Any help would be appreciated.
Upvotes: 2
Views: 104
Reputation:
You can use the Get-Variable
cmdlet for this:
write-host (Get-Variable foo$bar -Value).length
Demo:
PS > $bar = 1
PS > New-Variable "foo$bar" "hello"
PS > write-host (Get-Variable foo$bar -Value).length
5
PS >
Upvotes: 2