I am a beginner in PowerShell. I am trying to get the two lines of code below that I found here on another answer to run in PowerShell 5.
$netAdapter = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.MACAddress -eq '0F:98:90:D6:42:92'}
$netAdapter.EnableStatic("192.168.1.1", "255.255.255.0")
Is there additional code required for this to properly execute?
I get the error message "You cannot call a method on a null-valued expression." when I try to use it. I replaced the MAC address and IP address with the data for my device and network. I thought that maybe the second command returns a value, but I couldn't figure out the data type if that is the problem.
In the past I have used Bootp for IP address assignments where a non-PC device only has a MAC address with no valid IP address. Bootp isn't showing the broadcast for my device but I can see it in WireShark.
Before set the IP address to the interface, you must know the properties of the interface. To know the interface properties use the below command.
Get-NetIPAddress
It will show the output as

If you have multiple interfaces you will get the list of interface properties.
From the properties, you can note down the interface index value. then you can use the below command to set the IP address.
If you are setting the IP address first time the use command as
New-NetIPAddress -IPAddress 192.168.10.100 -PrefixLength 24 -DefaultGateway 192.168.10.1 -InterfaceIndex 4
To modify the existing static IP address use below command
Set-NetIPAddress -IPAddress 192.168.10.100 -PrefixLength 24 -DefaultGateway 192.168.10.1 -InterfaceIndex 4
To Set the Primary DNS & Secondary DNS use below command
Set-DnsClientServerAddress -InterfaceIndex 4 -ServerAddresses ('4.2.2.2','8.8.8.8')
To get the updated output for IP address and DNS use below command
Get-DnsClientServerAddress
Get-NetIPAddress
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With