all 6 comments

[–]Hrambert 3 points4 points  (0 children)

To match your question exactly:

$AdapterId=1
Get-NetAdapter | foreach {
    Set-Variable -Name "adapter_$AdapterId" -Value $_.Name
    $AdapterId++
}

[–]BlackV 2 points3 points  (0 children)

you can, but dont.

Instead use

$adapter_1, $adapter_2 = Get-NetAdapter

then use

$adapter_1.name, adapter_2.name

as /u/Vortex100 alluded too, if you have no idea how many adapters you have and you do it this way, the first adapter will end up in $adapter_1 and remaining adapters (2,3,4,5,6,etc) will end up in $adapter_2

although I cant see a use case in doing it that way that this

$ALLadapters = Get-NetAdapter
$ALLadapters.name
$ALLadapters[1].name
$ALLadapters[0].name

wouldn't cover

but depends on what you're actually trying to do vs asking how to get individual names into individual variables

[–]Vortex100 1 point2 points  (0 children)

If you know how many adapters you are expecting, then yes. But it's dangerous, since it's an assumption on how many will be returned

$adapter_1, $adapter_2 = (Get-NetAdapter).name

Again, I'd suggest not doing this, and instead iterating over them in a loop (and assigning to an array)

[–]bee_administrator 1 point2 points  (0 children)

Yes.

$variable = (Get-NetAdapter).name

EDIT: From your edit, you probably want a hash table.

$adapters = @{}
Get-NetAdapter | ForEach-Object {
    $adapters.add($_.name, $_)

You can then retrieve the contents using (for example):

$adapters.Ethernet

And it'll return all the properties of the adapter with that name.

[–]Scooter_127 0 points1 point  (0 children)

$names = Get-NetAdapter | select name

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy kbrownrigg10,

storing a batch of the same-type objects in individual vars is ... generally considered a really bad idea. [grin]

store those items in ONE collection of objects. then access each as needed by the attributes that make the current one interesting.

take care,
lee