Search code examples
powershellscriptingipv4netsh

Powershell netsh automatically run script


Is there a way to input text to notepad stating an IP Address, Subnet mask and Gateway and then running a powershell script that would execute the following netsh interface ipv4> set address name="Wireless Network Connection" source=static addr=192.xxx.x.xx mask=255.255.0.0 gateway=xxx.xxx.x.x gwmetric=0

At the moment I just type that manually in a Powershell terminal or cmd and it works (changes what i want changed) but i want to be able to just edit a notepad file and exute a batch file that would automatically do it..

i am pretty new to powershell and scripting but if someone can point me in the right direction it would be hugely appreciated.

Thanks.


Solution

  • There are many ways for you to do this. Here is one:

    ip_properties.txt contents:

    192.168.1.15,255.255.255.0,192.168.1.1
    

    powershell script contents:

    $properties = (Get-Content ip_properties.txt) -split ','
    Invoke-Expression "netsh interface ip set address name='Wireless Network Connection' source=static addr=$($properties[0]) mask=$($properties[1]) gateway=$($properties[2]) gwmetric=0"
    

    Essentially you're supplying your interface parameters in a comma separated format, splitting the those properties into an array and then using the array elements as inputs to the netsh command