cab get variable name?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jason@sbtc.com.tw
Posts: 26
Last visit: Wed Jan 17, 2024 11:18 pm

cab get variable name?

Post by jason@sbtc.com.tw »

i hope can get variable name, but no idea, script like below.

$123 = "test 123"
$456 = "test 456"
$789 = "test 789"
$a = "$123", "$456", "$789"
foreach ($t0 in $a)
{
# want to get variable name, not value
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: cab get variable name?

Post by jvierra »

The request is quite vague. Are you asking how to get the variable as a string.

Code: Select all

PS>$myname = 'Joe Smith'
PS> $varname = '$myname' -replace '\$'
PS> $varname
myname
PS>Get-Variable "$varname"
Name                           Value
---- -----
varname                        myname
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked