PowerShell Alternative of Telnet Command

PowerShell Alternative of Telnet Command

If you are a Windows System Administrator, you will surely know the importance of Telnet command in your day-to-day life. As you know that all version of Windows starting from Vista (and above) does not come with Telnet client feature enabled by default.

The syntax of Telnet is fairly simple as shown below:

Telnet 192.168.0.2 80

If you want to test the connectivity or reachability to some computer, either you have to enable the feature, or you will look for some alternative command to Telnet.

Luckily, every version of Windows now comes with PowerShell installed out of the box. So today I am going to show you the PowerShell alternative of Telnet command.

Launch the PowerShell console and type the following command:

New-Object System.Net.Sockets.TcpClient("192.168.0.2", 80)

In the above command, 192.168.0.2 is the IP of server (can be a hostname or domain-name like techtutsonline.com) you want to test and 80 is the Port number.

Take a look at Connected : True property. True means connection to server is successful. If you see False, means your PC cannot connect to server on specified port.

Creating PowerShell Module

If you can’t remember long commands, here is a simple method for you. You can create a PowerShell module and then this command will behave like an in-built PowerShell cmdlet.

Copy and paste the following code to a text file and save it with psm1 extension.

 

<#
.Synopsis
Tests the connectivity between two computers on a TCP Port

.Description
The Test-OpenPort Cmdlet Tests the connectivity between two computers on a TCP Port. By running this command, you can determine if specific service is running on Server.

.Parameter <ComputerName>
This is a required parameter where you need to specify a computer name which can be localhost or a remote computer

.Parameter <Port>
This is a required parameter where you need to specify a TCP port you want to test connection on.

.Example
Test-OpenPort -ComputerName DC1 -Port 3389
This command reports if DC1 can be connected on port 3389 which is default port for Remote Desktop Protocol (RDP). By simply running this command, you can check if Remote Desktop is enabled on computer DC1.

.Example
Test-OpenPort WebServer 80
This command tells you if WebServer is reachable on Port 80 which is default port for HTTP.

.Example
Get-Content C:\Computers.txt | Test-OpenPort -Port 80
This command will take all the computernames from a text file and pipe each computername to Test-OpenPort Cmdlet to report if all the computers are accessible on Port 80.
#>
Function Test-OpenPort{

[CmdletBinding()]
 param(
 [Parameter(Mandatory=$true,
 ValueFromPipeline=$true,
 ValueFromPipelineByPropertyName=$true)]
 [Alias ('HostName','cn','Host','Computer')]
 [String]$ComputerName='localhost',
 [Parameter(Mandatory=$true,
 ValueFromPipeline=$true,
 ValueFromPipelineByPropertyName=$true)]
 [int32]$Port
 )

Begin {}

Process {
 foreach($Computer in $ComputerName) {
 Try {
 $tcp = New-Object System.Net.Sockets.TcpClient
 $tcp.connect($Computer, $Port)
 Write-Host "Successfully connected to $Computer on Port: $Port" -ForegroundColor Green
 $tcp.close() 
 }

 Catch {
 Write-Host "Could not connect to $Computer on Port: $Port" -ForegroundColor Red
 }

}

 }
 End {}
}

To Create a Windows PowerShell module, follow these steps:

Create a folder named “Windows PowerShell in your [My] Documents folder.

Create another folder named “Modules inside Windows PowerShell folder.

Create another folder named “Test-OpenPort” inside Modules folder.

Save the code copied to notepad into Test-OpenPort folder with the file name “Test-OpenPort.psm1“. Note the file extension should be .psm1 which will make it a PowerShell module.

At the end, your PowerShell module should be located as shown in image below:

Thats it! Now open Windows PowerShell console on your PC and type the following command:

Get-Help Test-OpenPort -Full

You will notice that you have just created a Windows PowerShell cmdlet that will feel and behave like other cmdlets. You can use parameters like -ComputerName, -Port -Credential, -Verbose, -ErrorAction etc. which is awesome.

Below is the snapshot of Get-Help Test-OpenPort -Full command:

NAME
 Test-OpenPort

SYNOPSIS
 Tests the connectivity between two computers on a TCP Port

SYNTAX
 Test-OpenPort [-ComputerName] <String> [-Port] <Int32> [<CommonParameters>]

DESCRIPTION
 The Test-OpenPort Cmdlet Tests the connectivity between two computers on a TCP Port. By running this command, you can determine
 if specific service is running on Server.

PARAMETERS
 -ComputerName <String>

Required? true
 Position? 1
 Default value localhost
 Accept pipeline input? true (ByValue, ByPropertyName)
 Accept wildcard characters? false

-Port <Int32>

Required? true
 Position? 2
 Default value 0
 Accept pipeline input? true (ByValue, ByPropertyName)
 Accept wildcard characters? false

<CommonParameters>
 This cmdlet supports the common parameters: Verbose, Debug,
 ErrorAction, ErrorVariable, WarningAction, WarningVariable,
 OutBuffer, PipelineVariable, and OutVariable. For more information, see
 about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS

-------------------------- EXAMPLE 1 --------------------------

PS C:\>Test-OpenPort -ComputerName DC1 -Port 3389

This command reports if DC1 can be connected on port 3389 which is default port for Remote Desktop Protocol (RDP). By simply
 running this command, you can check if Remote Desktop is enabled on computer DC1.

-------------------------- EXAMPLE 2 --------------------------

PS C:\>Test-OpenPort WebServer 80

This command tells you if WebServer is reachable on Port 80 which is default port for HTTP.

-------------------------- EXAMPLE 3 --------------------------

PS C:\>Get-Content C:\Computers.txt | Test-OpenPort -Port 80

This command will take all the computernames from a text file and pipe each computername to Test-OpenPort Cmdlet to report if
 all the computers are accessible on Port 80.

RELATED LINKS

Using Test-OpenPort Cmdlet

Now, its time to use our newly created PowerShell Module. Open up the new PowerShell console session.

As shown in examples of cmdlet, you can use the PowerShell module in 3 different ways.

First Way:

PS D:\MyScripts> Test-OpenPort -ComputerName techtutsonline.com -Port 80
Successfully connected to techtutsonline.com on Port: 80
PS D:\MyScripts>

Second Way:

PS D:\MyScripts> Test-OpenPort techtutsonline.com 80
Successfully connected to techtutsonline.com on Port: 80
PS D:\MyScripts>

Third Way:

PS D:\MyScripts> Get-Content D:\Servers.txt | Test-OpenPort -Port 80
Successfully connected to WebServer on Port: 80
Could not connect to DC1 on Port: 80
Successfully connected to Linux-Box on Port: 80
PS D:\MyScripts>

Congratulations! your PowerShell module is working as expected.

This cmdlet is an ideal way to work on client’s servers where you do not have authority (or you don’t want for any reason) to enable Telnet-Client Windows feature.



<p>Microsoft Certified Professional | Cisco Certified Network Associate</p>

Leave a Reply