none
Powershell - using netsh interface name as a variable RRS feed

  • Question

  • Hi all,

    I'm writing a script for change a Dhcp IP into a static one for a large amount of windows clients ranging from win XP to win 10.

    long story short, I'm able to manage the request with Get-NetAdapter for the clients with win 8 and the newer ones:

    $NAME=(Get-NetAdapter -physical | Where-Object {$_.Status -eq "Up"} | Select-Object Name -ExpandProperty Name)

    nevertheless, for the older clients, I'm not able to find the interface name to pass at netsh in order to change the parameters using powershell to parse the output.

    My goal is to use something like this:

    netsh interface ipv4 set address name="$NAME" static $ip 255.255.255.0 192.168.1.254 1

    Please, don't blame me, I'm a new bie here :)

    Thanks to everyone that will help me.

    Thursday, July 18, 2019 10:24 AM

Answers

  • Try to fetch the details using the wmi class:

    Get-WmiObject Win32_NetworkAdapter | ?{$_.NetConnectionStatus -eq 2 -and $_.PhysicalAdapter -eq $true}| Select -ExpandProperty NetConnectionID

    • Marked as answer by Miralem Thursday, July 18, 2019 11:55 AM
    Thursday, July 18, 2019 10:58 AM

All replies

  • Try to fetch the details using the wmi class:

    Get-WmiObject Win32_NetworkAdapter | ?{$_.NetConnectionStatus -eq 2 -and $_.PhysicalAdapter -eq $true}| Select -ExpandProperty NetConnectionID

    • Marked as answer by Miralem Thursday, July 18, 2019 11:55 AM
    Thursday, July 18, 2019 10:58 AM
  • Brilliant! 

    Thank you so much!

    Thursday, July 18, 2019 11:56 AM
  • or with another native cmdlet without WMI (or CIM) request

    Get-NetAdapter | where {($_.physicalmediaType -eq "802.3") -and ($_.virtual -ne $True)} | Select-Object -Property Name
    Get-NetAdapter | where {($_.physicalmediaType -eq "802.3") -and ($_.status -eq "up")}   | Select-Object -Property Name


    Match too : Only Wired NICs (no wi-fi or bluetooth or whatever) and enabled.




    Thursday, July 18, 2019 1:41 PM