Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if there is an active network connection in powershell?

I want part of my script to only run if the computer has any established network connection. It can be wifi, ethernet, anything that's not virtual. I don't need full internet, just LAN.

This is all for Windows 10 64bit, and this script is part of a larger script I'm going to use for deploying some machines.

Note: I'm definitely a powershell noob, I'm not very familiar with it. Honestly I'm still a noob with programming and scripting in general, but powershell especially.

I think I need some function to check each network adapter individually, but I'm not sure how to do that in powershell.

if (Get-NetAdapter -Name "*" -Physical == "Up") {
    Write-Host "Pass"
}
else {
    Write-Host "Fail"
}

I hoped my code would maybe check each option in the list of Get-NetAdapter, but it gave an error on the == argument, it can't find a positional parameter that accepts it. I also tried = which gave the same error.

like image 840
udlp Avatar asked Sep 05 '25 20:09

udlp


1 Answers

Examplecode to check an array of network adapter

$networkavailable = $false;
foreach ($adapter in Get-NetAdapter){
  if ($adapter.status -eq "Up"){$networkavailable = $true; break;}
}
like image 135
Ronny Kaufmann Avatar answered Sep 08 '25 22:09

Ronny Kaufmann