# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action of Hexnode UEM.

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

List IP addresses
-----------------

Use the following script to retrieve all the IP addresses in use on the endpoint:

PowerShell script to list IP addresses

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExpandProperty IPAddress 

   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
--------------------------

PowerShell script to list IP configuration data

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter IPEnabled=$true 

   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.

PowerShell script to list IP configuration data excluding specific properties

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExcludeProperty IPX\*,WINS\* 

   1

  Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExcludeProperty IPX\*,WINS\* 

   

 

  

List network adapter properties
-------------------------------

PowerShell script to list network adapter properties

Get-NetAdapter 

   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
--------------------------------------

PowerShell script to assign DNS domain for network adapters

$wql = 'SELECT \* FROM Win32\_NetworkAdapterConfiguration WHERE IPEnabled=True' $arg = @{ DnsDomain = 'example.com'} Invoke-CimMethod -MethodName SetDNSDomain -Arguments $arg -Query $wql 

   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.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.