JeffHarbert
JeffHarbert

Reputation: 23

How to return the length of a new variable created with New-Variable

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

Answers (1)

user2555451
user2555451

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

Related Questions