PS: Networking Commands

4 minute read


I still mainly use CMD commands for these, but this is a list of networking related commands you can do from Powershell:

To Check If A Port Is Open (Quit using Telnet! It’s 2018!!)

   Test-NetConnection -port 445
   # Look for the TCPTestSucceeded to be "True"
   # This shows that TCP packets can traverse as opposed to just ICMP packets
   # I use this one almost daily

   # Test-NetConnection for Server2008r2:
   $tcp = New-Object Net.Sockets.TcpClient 
   $tcp.Connect("", 443)

To Interact With REST API’s

   # Rest method examples
   Invoke-RestMethod -Uri '' -Method Get | 
   Select-Object -ExpandProperty Value
   Invoke-RestMethod -Uri '' -Method Get | 
   Select-Object -ExpandProperty Fact

Or Just Regular Sites Like SS64:

   Function Get-Help2
   Launches a window in for a selected command.
   Launches a window in for a selected command.
   Get-Help Get-Process
   Opens a window in your default internet browser to ss64's page on get-process.
   2017-10-19: v1.0 Initial script 
      $command = $command.ToLower()
      Start-process -filepath "$command.html"

To Disable DHCP On A NIC (W8/Server 2012):

   Get-NetAdapter -Name "Local Area Connection" | Set-NetIPInterface -DHCP Disabled

To Set A Static IP and Default Gateway (W8/Server 2012):

   Get-NetAdapter -Name "Local Area Connection" | New-NetIPAddress -AddressFamily IPv4 -IPAddress -PrefixLength 24 -Type Unicast -DefaultGateway

To Configure DNS Server Address (W8/ Server 2012):

   Get-NetAdapter -Name "Local Area Connection" | Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses

To Map A Network Drive:

   New-PSDrive -Name S -Root \Server01Scripts -Persist -PSProvider FileSystem
  • This creates a S: visible in Windows Explorer that is a permanent mapped drive.

To Get Public IP:

   $publicIP = (Invoke-WebRequest -uri
   # $publicIP can now be used to see your WAN IP.

To Install Exe’s:

   Foreach ($Computer In $Computers)
   Copy-Item C:\Yourlocalscript.Ps1 -Destination \\$Computer\c$\Temp
   Invoke-Command -Scriptblock { Powershell.Exe C:\Tempcscript.Ps1 } -Computername $Computer -Asjob

To Download A File:

   Invoke-Webrequest -Outfile $Env:Userprofile\Downloads\Example.Zip

To Download To Temp Directory

   Function DownloadFileToTemp

      $fileName = []::GetFileName($url)
      $tmpFilePath = []::Combine($env:TEMP, $fileName)

      $wc = new-object System.Net.WebClient
      $wc.DownloadFile($url, $tmpFilePath)

      return $tmpFilePath

To Get Computer/ Domain Names:

   (Get-Wmiobject Win32_Computersystem).Name
   (Get-Wmiobject Win32_Computersystem).Domain

To Send Mail:

   Function Sendmailmessage
   $From = “”
   $To = “”
   $Mailserver = “”
   Send-Mailmessage -Smtpserver $Mailserver -From $From -To $To -Subject $Subject -Body $Body
   # Be Sure That Your Mail Server Accepts Mails From The Host

To Disable and Enable a NetAdapter

   Disable-NetAdapter -Name "Wireless Network Connection"
   Enable-NetAdapter -Name "Wireless Network Connection"

To Set a static IP

   New-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address -PrefixLength "24" -DefaultGateway
   # or if existing
   Set-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address -PrefixLength "24"
   Set-NetIPInterface -InterfaceAlias "Wireless" -Dhcp Enabled

To Check for a port opening:

   Test-NetConnection -ComputerName -Port 80
   Test-NetConnection -ComputerName -CommonTCPPort HTTP

To SMB Information:


To Set Hyper-V Settings:


   # Disable VMQ
   Set-NetAdapterVmq -Enabled $false

   # Enable VMQ
   Set-NetAdapterVmq -Enabled $true

   # For a specific VM
   Get-VMNetworkAdapter -VMName Server01

   # Get VM Network Adapter IP Addresses
   (Get-VMNetworkAdapter -VMName NanoConHost01).IPAddresses

   # Get VM Network Adapter Mac Addresses
   (Get-VMNetworkAdapter -VMName NanoConHost01).MacAddress

PS Remoting

To Use local variables in Remote Commands

   LocalVar = "test"
   Invoke-Command -ComputerName ms2 -ScriptBlock {Write-host "The localvar value $using:LocalVar"}

   # Map a drive in a remote session:
   $getcred = Get-Credential 'myuser'
   $servers = "myserver"
   $jsession = New-PSSession -ComputerName $servers -Credential $getcred
   Invoke-Command -ScriptBlock {
      # You will have to enable CredSSP if you don't use the $Using variable here.
      New-PSDrive -name share1 -psprovider FileSystem -root \\server\directory -Credential $using:getcred;
      dir share1:

To Install Remote MSI

   Import-Csv -Path "C:\temp\New_relicInst.csv" | ForEach-Object {
      $vmhost = $_.Name
      $dest = "\\" + $vmhost + "\C$\Windows\temp"
      copy-item " WindowsAzureVmAgent.2.7.1198.778.rd_art_stable.160617-1120.fre.msi" -Destination $dest -Force
      Invoke-Command -ComputerName $vmhost -ScriptBlock {
         $Exp = "cmd.exe /c C:\windows\temp\WindowsAzureVmAgent.2.7.1198.778.rd_art_stable.160617-1120.fre.msi /q /l* C:\windows\temp\azureagentinst.log"
         Invoke-Expression $Exp

To Spawn Remote Session In New Window

   Start-Process -FilePath 'PowerShell.exe' -ArgumentList '-NoExit',"-command `"Enter-PSSession -ComputerName $ComputerName`""

To Get A Remote Reg Value

   Invoke-Command -ComputerName myserver -Credential (Get-Credential) -ScriptBlock {

   #1 means enabled.

  1. 2018-04: Adding additional notes here…

  2. Commands:

    • IPCONFIG – PowerShell: Get-NetIPConfiguration or Get-NetIPAddress
    Get-NetIPAddress | Sort InterfaceIndex | FT InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress, PrefixLength -Autosize
    Get-NetIPAddress | ? AddressFamily -eq IPv4 | FT AutoSize
    Get-NetAdapter Wi-Fi | Get-NetIPAddress | FT -AutoSize
    • IPCONFIG/RELEASE and IPCONFIG /RENEW – I would place in a function like:
    $DHCPAdapters = Get-WmiObject Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true } 
    foreach ($DHCP in $DHCPAdapters)
    $DHCP.ReleaseDHCPLease() | Out-Null
    Start-Sleep -Seconds 1
    $DHCP.RenewDHCPLease() | Out-Null
    • PING – PowerShell: Test-NetConnection
    Test-NetConnection -ComputerName -InformationLevel Detailed
    Test-NetConnection -ComputerName | Select -ExpandProperty PingReplyDetails | FT Address, Status, RoundTripTime
    1..10 | % { Test-NetConnection -ComputerName -RemotePort 80 } | FT -AutoSize
    • Get some more details from the Test-NetConnection
    Test-NetConnection -ComputerName -InformationLevel Detailed
    • Ping multiple IP using PowerShell
    1..99 | % { Test-NetConnection -ComputerName x.x.x.$_ } | FT -AutoSize
    • NSLOOKUP – PowerShell: Resolve-DnsName
    Resolve-DnsName -type SOA
    Resolve-DnsName -Server Type A
    • Bulk NSLOOKUP
    $servers = Get-Content "c:\_gwill\prod2.txt"
    Foreach ($s in $servers)
       Write-Output "checking $s"
       $cmd = nslookup $s
       If ($Cmd.Length -eq 3)
       Write-Output $s | Out-file "c:\_gwill\err.txt" -Encoding ascii -Append
       Start-Sleep -Milliseconds 10
    • ROUTE – PowerShell: Get-NetRoute (also New-NetRoute and Remove-NetRoute)
    Get-NetRoute -Protocol Local -DestinationPrefix 192.168*
    Get-NetAdapter Wi-Fi | Get-NetRoute
    • TRACERT – PowerShell: Test-NetConnection –TraceRoute
    Test-NetConnection TraceRoute
    Test-NetConnection -TraceRoute | Select -ExpandProperty TraceRoute | % { Resolve-DnsName $_ -type PTR -ErrorAction SilentlyContinue }
    • NETSTAT – Powershell Get-NetTCPConnection
    Get-NetTCPConnection | Group State, RemotePort | Sort Count | FT Count, Name Autosize
    Get-NetTCPConnection | ? State -eq Established | FT Autosize
    Get-NetTCPConnection | ? State -eq Established | ? RemoteAddress -notlike 127* | % { $_; Resolve-DnsName $_.RemoteAddress -type PTR -ErrorAction SilentlyContinue }