Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
General PowerShell
use 1 variable to define the name of another variable
Last Post 10 Sep 2010 01:02 PM by
Shay Levy
. 5 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
thanosazlin
New Member
Posts:19
09 Sep 2010 10:01 AM
new to powershell still learning. just wondering how would i take a variable and use it to define the name of another variable.
example:
$a = 1
$test"$a" = "testing code"
so that i would have a variable called $test1 that would contain "testing code"???
i also tried $test'$a' not able to google the right keywords to find anything to help me out :) .
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
09 Sep 2010 01:06 PM
When you embed a variable in a string powershell expand it and you get the value (not the name). Try this:
new-variable -name ("test"+(get-variable a).name) -value "testing code"
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
thanosazlin
New Member
Posts:19
10 Sep 2010 07:10 AM
thanks for that, you posts are always helpful :).. i couldn't get it work, it was creating a var named $testa instead of $test1 which the 1 is the value of a, so i tried the below code and it worked using -valueonly
$a = 1
new-variable -name ("test"+(get-variable a -valueonly)) -Value "testing code"
$test1
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
10 Sep 2010 12:45 PM
Cool, I though you wanted the variable name :)
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
cameronove
Basic Member
Posts:332
10 Sep 2010 12:55 PM
New-Variable -name "test$a" -Value "Testing Code"
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
10 Sep 2010 01:02 PM
Exactly! :)
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2