, , , ,

Replace netsh with Windows PowerShell – Basic network cmdlets

Some days ago I made a blog post about how you can replace diskpart.exe with the new cmdlets in Windows PowerShell v3. Now my next goal was to replace netsh with PowerShell.

Microsoft offers a lot of new cmdlets for networking tasks lets see what we have here. We got a lot of new PowerShell modules which are related to networking.

get-module

Now the two modules which sound really interesting are

NetAdapter
NetTCPIP

lets check which cmdlets are included

Get-Command -Module NetAdapter

net-adpater module

 Get-Command -Module NetTCPIP

Now lets start with NetAdpater.

List all Network Adapters

 Get-NetAdapter 

get-netadapter

Just list Wireless Network Adapters

 Get-NetAdapter -Name *Wireless* 

get-netadapter wireless

Get more information VLAN ID, Speed, Connection status

 Get-NetAdapter | ft Name, Status, Linkspeed, VlanID 

get-netadapter information

Get driver information

 Get-NetAdapter | ft Name, DriverName, DriverVersion, DriverInformation, DriverFileName 

get-netadapter driver

Get adapter hardware information. This can be really usefull when you need to know the PCI slot of the NIC.

 Get-NetAdapterHardwareInfo 

get-netadapterhardwareinfo

Disable and Enable a Network Adapter

 Disable-NetAdapter -Name "Wireless Network Connection"
Enable-NetAdapter -Name "Wireless Network Connection" 

disable enable network adapter

Rename a Network Adapter

 Rename-NetAdapter -Name "Wireless Network Connection" -NewName "Wireless" 

rename network adapter

Now with the NetTCPIP PowerShell module we can work with IP settings

Get IP and DNS address information

 Get-NetAdapter -Name "Local Area Connection" | Get-NetIPAddress 

get-ipaddress

Get IP address only

 (Get-NetAdapter -Name "Local Area Connection" | Get-NetIPAddress).IPv4Address 

get-ipaddress only

Get DNS Server Address information

 Get-NetAdapter -Name "Local Area Connection" | Get-DnsClientServerAddress 

get-dnsserver

Set IP Address

 New-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 10.0.1.95 -PrefixLength "24" -DefaultGateway 10.0.1.1 

or if you want to change a existing IP Address

 Set-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 192.168.12.25 -PrefixLength "24"

Remove IP Address

 Get-NetAdapter -Name "Wireless" | Remove-NetIPAddress 

Set DNS Server

 Set-DnsClientServerAddress -InterfaceAlias "Wireless" -ServerAddresses "10.10.20.1","10.10.20.2" 

Set interface to DHCP

 Set-NetIPInterface -InterfaceAlias "Wireless" -Dhcp Enabled 

Now with this basic knowledge we can replace some of the basic netsh commands and ipconfig

netsh

ipconfig
netsh interface set interface "Local Area Connection" newname="Management"
netsh interface ipv4 set address "Management" static 192.168.10.101 255.255.255.0 192.168.10.1

Windows PowerShell

Get-NetAdapter
Rename-NetAdapter -Name "Local Area Connection" -NewName "Management"
New-NetIPAddress -InterfaceAlias "Management" -IPv4Address 192.168.10.101 -PrefixLength "24" -DefaultGateway 192.168.10.1

15 responses

  1. Erez Avatar

    Hi,
    I have PowerShell 3.0 but don’t see these modules.
    Do I need to download them?

    1. Thomas Maurer Avatar

      maybe you have started PowerShell v2? Try to start powershell with “powershell.exe -Version 3.0”

  2. Erez Avatar

    Checked again its version 3.0.

    PS C:\Users\gabbay> get-host

    Name : ConsoleHost
    Version : 3.0
    InstanceId : 5988fa36-2f54-43ce-99cd-3cb9a6dd9748
    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : en-US
    CurrentUICulture : en-US
    PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    IsRunspacePushed : False
    Runspace : System.Management.Automation.Runspaces.LocalRunspace

  3. Thomas Maurer Avatar

    Do you run PowerShell v3 in Windows 8 or Windows 7.
    I used Windows 8 :)

  4. Erez Avatar

    Both: Win 7 SP1 and 2008 R2 SP1

    1. Thomas Maurer Avatar

      Looks like it is just working with Windows 8 and Windows Server 2012 at the moment. Maybe with the final Version of PowerShell v3 it could work with Windows 7 but I would not bet my Money ;-)

  5. Erez Avatar

    Thanks

  6. Nikhil Avatar
    Nikhil

    Thomas,

    This is a great post to come across and read about the two new Modules described.

    I’m using PowerShell Version 2, on windows 7. When querying for the modules present in my set-up, I see an empty 2nd column, that shows the commands in the module.

    PowerCLI C:\pcli> Get-Module -ListAvailable

    This is o/p from my powershell cmd.
    ModuleType Name ExportedCommands
    ———- —- —————-
    Manifest PSNetAdapterConfig {}
    Manifest AppLocker {}
    Manifest BitsTransfer {}
    Manifest PSDiagnostics {}
    Manifest TroubleshootingPack {}

    Can you tell me what could have gone wrong, which is why it’s not showing that.

    1. Thomas Maurer Avatar

      This guides are made for PowerShell v3 on Windows 8 and Windows Server 2012 :)

  7. Petr Avatar

    what classic cmd command ” netsh int ip reset ” this do not work on win server 2012 R2 anymore

  8. E. Sams Avatar
    E. Sams

    Thanks for the detail syntax. I am using win8.1 Pro ; and I am trying to move my machine from public to private profile.

  9. Niklas Avatar

    Now all we need is something to replace the route command….in a nice way like route print. :)

  10. Javier Avatar

    Hi Thomas
    Thanks for your article!

    Frage: is there a way to “uninstall” the Driver for a specific NIC? via powershell or CLI?

    TIA

    Javier :)

  11. Rohith Avatar

    HI Thomas Maurer,
    Is there any command in powershell equivalent to http add urlacl from netsh?

  12. Peter Monadjemi Avatar
    Peter Monadjemi

    What about URL reservation like

    netsh http add urlacl http://+:8082/E2A_EF_Webservice/EFService/

    What is the Powershell equivalent?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.