Get-NetConnectionProfile for Windows 7

As customers are still working with Windows 7 and PowerShell (PS) versions before 3.0, sometimes there are work a rounds needed. You can upgrade your PS version on Windows 7 to a newer version, but that’s not always an option for everyone.

One of the newer cmdlets available in PS 3+ is the Get-NetConnectionProfile that gets a connection profile associated with one or more physical network adapters. A connection profile represents a network connection.

If you don’t have the luxury of updating PS on a Windows 7 machine, I worked up this little function to help people, leveraging the underlying .NET process from the INetwork:Getname method. Just copy/paste the code into ISE, or Visual Studio, or add it into your profile, or even add it into a module on the machine you need it to run. This allows you to reuse the code as needed. I didn’t add any additional code to remote into other machines, maybe in the future if there is enough need.

Function Get-NetConnectionProfileWin7 {
<#
.Synopsis
Works in Windows 7 with PS 2
.DESCRIPTION
Function that gets a connection profile associated with one or more physical network adapters.
A connection profile represents a network connection.
.EXAMPLE

Get-NetConnectionProfileWin7

.NOTES
Created for Windows 7 and PS 2.0+.
#>

$NetworkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID(‘DCB00C01-570F-4A9B-8D69-199FDBA5723B’))

$NLM_ENUM_NETWORK_CONNECTED = 1
$NLM_ENUM_NETWORK_DISCONNECTED = 2
$NLM_ENUM_NETWORK_ALL = 3

foreach($net in $NetworkListManager.GetNetworks($NLM_ENUM_NETWORK_CONNECTED))

{

Write-Host "Name: "$net.GetName() -ForegroundColor Green

Write-Host "Status of connection: " $net.IsConnected

Write-Host ""

}

}

 

Here is a .ps1 file zipped up if you want the code in this format.

Get-NetConnectionProfileWin7