How to Change Your IP Address Using PowerShell V3

image477

Note: The following commands are new in PowerShell v3 and therefore require Windows 8, they also require an administrative command prompt.

Changing Your IP Address

We have seen people pulling out their hair trying to change their IP addresses using cryptic WMI classes in older versions of PowerShell, but that changed with PowerShell v3, there is now a NetTCPIP module that brings most of the functionality to native PowerShell. While a bit confusing at first, mostly due to the lack of documentation at the moment, it starts to make sense once the geeks shows you how its done.

Changing an IP Address can be done using the New-NetIPAddress cmdlet, it has a lot of parameters, some of which, are not even documented in Get-Help. So here it is:

New-NetIPAddress –InterfaceAlias “Wired Ethernet Connection” –IPv4Address “192.168.0.1” –PrefixLength 24 -DefaultGateway 192.168.0.254

This assumes the following:

  • The name of the interface you want to change the IP address  for is Local Area Network
  • You want to statically assign an IP address of 192.168.0.1
  • You want to set a subnet mask of 255.255.255.0 (which is /24 in CIDR notation)
  • You want to set a default gateway of 192.168.0.254

You would obviously switch the settings out for some that match the addressing criteria for your network.

Setting Your DNS Information

Now here comes another tricky part, it turns out that there is a whole separate module called DNSClient that you have to use to manipulate your DNS Settings. To change your DNS Server you would use:

Set-DnsClientServerAddress -InterfaceAlias “Wired Ethernet Connection” -ServerAddresses 192.168.0.1, 192.168.0.2

This assumes that you want to set the primary DNS server for Wired Ethernet Connection to 192.168.0.1 and the secondary DNS server to 192.168.0.2. That’s all there is to it.

~source

$PSItem in PowerShell v3

In PowerShell, the variable “$_” has special importance. It works like a placeholder in scenarios like this one:

PS> Get-ChildItem $env:windir | Where-Object { $_.Length -gt 1MB }

In PowerShell v3, there is an alias for the cryptic “$_”: $PSItem. So now code can become more descriptive:

PS> Get-ChildItem $env:windir | Where-Object { $PSItem.Length -gt 1MB }

Then again, in PowerShell v3, “$_” isn’t necessary in many scenarios anymore at all. You could also write:

Get-ChildItem $env:windir | Where-Object Length -gt 1MB