Using PowerShell to Check if a Port is Open and export results to an Excel file(Cross-Platform…

PowerShell is a powerful cross-platform tool available on Windows, macOS, and Linux. While some PowerShell cmdlets, such as…

Using PowerShell to Check if a Port is Open and export results to an Excel file(Cross-Platform…
Photo by Edgar on Unsplash

Using PowerShell to Check if a Port is Open and export results to an Excel file(Cross-Platform: Windows, macOS, Linux)

PowerShell is a powerful cross-platform tool available on Windows, macOS, and Linux. While some PowerShell cmdlets, such as Test-NetConnection, are not supported outside Windows, you can still use PowerShell’s .NET capabilities to test if a port is open. This article shows you how to achieve this in any environment.

Why Check Ports?

Network ports are essential for communication between devices and services. For example:

  • Port 80/443: Web servers (HTTP/HTTPS)
  • Port 22: SSH
  • Port 3306: MySQL

If a port is closed, the service may be unreachable. Checking the port’s status can help troubleshoot connectivity issues.

Cross-Platform Solution: Using TcpClient

The TcpClient class from .NET works seamlessly across Windows, macOS, and Linux when testing network ports in PowerShell. This is a reliable alternative when Test-NetConnection is unavailable.

Script

Here’s a reusable PowerShell function to check if a port is open:

function Test-Port { 
    param ( 
        [string]$ComputerName,  # Hostname or IP of the target 
        [int]$Port              # Port number to test 
    ) 
    try { 
        $tcpClient = [System.Net.Sockets.TcpClient]::new() 
        $tcpClient.Connect($ComputerName, $Port) 
        Write-Host "Port $Port is open on $ComputerName" -ForegroundColor Green 
        $tcpClient.Close() 
        return $true 
    } catch { 
        Write-Host "Port $Port is closed on $ComputerName" -ForegroundColor Red 
        return $false 
    } 
} 
 
# Example Usage 
Test-Port -ComputerName "in.gr" -Port 443

How It Works

  1. The script uses the TcpClient class to attempt a connection to the specified host and port.
  2. If the connection is successful, it prints “Port is open.”
  3. If the connection fails, it prints “Port is closed.”

Example: Checking Ports

  1. Check Port 443 on a Web Server:
Test-Port -ComputerName "example.com" -Port 443

Output:

  • If the port is open:
    Port 443 is open on example.com
  • If the port is closed:
    Port 443 is closed on example.com

2. Check Multiple Ports: You can loop through multiple ports to test them all:

$ports = @(22, 80, 443) 
$computer = "example.com" 
 
foreach ($port in $ports) { 
    Test-Port -ComputerName $computer -Port $port 
}

Output for each port will indicate whether it is open or closed.

Exporting Port Check Results to an Excel File

If you’re testing multiple ports or hosts, it can be helpful to save the results in an Excel file for analysis or documentation. PowerShell makes this easy with the Import-Excel module or by leveraging .NET libraries.

Installing the Required Module

For exporting to Excel, the ImportExcel PowerShell module is a popular and easy-to-use choice. You can install it via PowerShell’s package manager.

Install-Module -Name ImportExcel -Scope CurrentUser -Force

Modified Script to Export Results

Here’s how you can modify the Test-Port script to save results into an Excel file:

# Ensure the ImportExcel module is installed first 
function Test-Port { 
    param ( 
        [string]$ComputerName,  # Hostname or IP of the target 
        [int]$Port              # Port number to test 
    ) 
    try { 
        $tcpClient = [System.Net.Sockets.TcpClient]::new() 
        $tcpClient.Connect($ComputerName, $Port) 
        $tcpClient.Close() 
        return [PSCustomObject]@{ 
            ComputerName = $ComputerName 
            Port         = $Port 
            Status       = "Open" 
        } 
    } catch { 
        return [PSCustomObject]@{ 
            ComputerName = $ComputerName 
            Port         = $Port 
            Status       = "Closed" 
        } 
    } 
} 
 
# Test multiple ports and export to Excel 
$results = @() 
$computer = "example.com" 
$ports = @(22, 80, 443) 
 
foreach ($port in $ports) { 
    $results += Test-Port -ComputerName $computer -Port $port 
} 
 
# Export results to Excel 
$results | Export-Excel -Path ./PortResults.xlsx -AutoSize 
Write-Host "Results exported to PortResults.xlsx" -ForegroundColor Green

How It Works

  1. Results Collection:
    The script loops through the specified ports, runs the Test-Port function for each, and collects the results in an array.
  2. Excel Export:
    The Export-Excel cmdlet from the ImportExcel module creates an Excel file (PortResults.xlsx) in the current directory with the results.

Conclusion

PowerShell’s cross-platform capabilities, combined with the .NET TcpClient class, make it a powerful tool for network diagnostics. With the script provided, you can check if a port is open on any machine running PowerShell, regardless of the operating system.

Try it out and let us know how it works for you! If you have other network-related tasks you’d like automated with PowerShell, share your ideas!