Category filter
PowerShell script to perform network tasks on Windows devices
This document explains how you can use PowerShell scripts to perform certain networking tasks on Windows devices. The tasks vary from fetching IP addresses, IP configuration data, and network adapter properties associated with the devices or assigning DNS domains for network adapters. But how are these tasks helping organizations?
For IT administrators, having a clear view of the IP addresses in use on the device, IP configuration data for each network adapter, and network adapter properties is crucial for maintaining robust network infrastructure. It can help troubleshoot connectivity issues in their Windows devices. Likewise, assigning the DNS domain for automatic name resolution on Windows helps streamline network access, improve user productivity, and ensure efficient name resolution in diverse network environments. You can use PowerShell scripts to perform these operations on Windows devices. IT admins can now remotely deploy these scripts to multiple endpoints using the Execute Custom Script action of Hexnode UEM.
List IP addresses
Use the following script to retrieve all the IP addresses in use on the endpoint:
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExpandProperty IPAddress |
To view the entire list of IP addresses, we filter IP-enabled network adapters and use the ExpandProperty
parameter of Select-Object
because the IPAddress property of a Win32_NetworkAdapterConfiguration object is an array.
List IP configuration data
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true |
The Get-CimInstance
cmdlet displays detailed IP configuration data for each network adapter in the device which are IP-enabled.
In case you are not interested in IPX or WINS properties in the TCP/IP networks, use the Select-Object
‘s ExcludeProperty
parameter to conceal properties whose names begin with “WINS” or “IPX.” The details of DHCP, DNS, routing, and other minor IP configuration properties are returned by this command.
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExcludeProperty IPX*,WINS* |
List network adapter properties
1 |
Get-NetAdapter |
Get-NetAdapter
cmdlet retrieves information about all network adapters on the endpoint. We extract various properties of each network adapter, such as the adapter name, interface description, interface index, status, MAC address, and link speed.
Assign DNS domain for network adapters
1 |
$wql = 'SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True' $arg = @{ DnsDomain = 'example.com'} Invoke-CimMethod -MethodName SetDNSDomain -Arguments $arg -Query $wql |
For assigning the DNS domain for network adapters, the Invoke-CimMethod
cmdlet uses the SetDNSDomain
method of the Win32_NetworkAdapterConfiguration. The Query
parameter of Invoke-CimMethod
takes a WQL query string. The cmdlet calls the method specified on each instance returned by the query. Because many of the network adapter configurations on a device may not be true TCP/IP adapters, even on a network that uses only TCP/IP, it would be best to filter those adapters that have IP enabled in the query.