Powershell – change ip address for 10 computers based on a txt file in powershell or vbscript

powershellvbscript

I have a txt file that looks like this

PC1 192.168.0.2  
PC2 192.168.0.3  
PC3 192.168.0.4

I would like to change computers ip addresses based on that list, so it will fill up the computer name and the ip address and process them all without manually changing the values, and I'm having issues getting this to work "still learning powershell"

$wmi = Get-WmiObject -ComputerName ??? win32_networkadapterconfiguration -filter "ipenabled = 'true'"

$wmi.SetGateways("192.168.0.1", 1)
$wmi.EnableStatic("?????", "255.255.255.0")

any help on that?

Best Answer

If your txt file is just a space-separated list of [computername] [ip-address], you can use the Import-Csv cmdlet:

$Computers = Import-Csv -Path C:\file.txt -Delimiter " " -Header Name,IP

foreach($Computer in $Computers){
    # Query $Computer.Name
    # Assign $Computer.IP as the static address
}