Powershell - Getting data, creating variables, and using it in a script.

I am learning PowerShell scripting. One of my stumbling blocks in learning is how to properly get data, and then using it later in the script.

For example. I need to get the GUID for a connected NIC. I can run the following which will get me the information.
get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select-object guid

However I would like to take that GUID store it in a variable to be called later. However when I do that I get additional information like:

This presents an issue when I attempt to use it a different part of the script like this:

$GUID = get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select-object guid
set-location hklm:\system\currentcontrolset\services\tcpip\parameters\interfaces\$GUID

Please help shed some light on how to achieve this.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Select-Object still returns an object, in this case with one property.
The solution is either to use "Select-Object -ExpandProperty ..." instead of "Select-Object -Property ...":
$GUID = get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select-object -ExpandProperty guid

Open in new window

Or you address the property directly:
$GUID = (get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2").GUID

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The latter works always with PS3 and above, and with PS 2 only if there is a single object as result. Getting more than one object is not unlikely.
For the second command, it may be worth mentioning that you can reference a property of an object stored as a variable.  For instance, within a string you would use a subexpression - $().
$GUID = get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | select-object guid
set-location "hklm:\system\currentcontrolset\services\tcpip\parameters\interfaces\$($GUID.guid)"

Open in new window

You do have to take care though to make sure you're dealing with only a single object.  If the first command returned more than one, then you would have to deal with each one individually, typically with a foreach loop or ForEach-Object cmdlet.
TechOps1Author Commented:
Thank you everyone for your help. This gets me going in the right direction.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.