PowerShell: test connection in as few characters as possible

PowerShell v2+, 63 bytes

You can use a different constructor to create the object and connect in one go.

(new-object Net.Sockets.TcpClient('192.168.5.5',445)).Connected

I've verified this works in v2 on my Windows 8.1 machine. That constructor is supported by .NET 2.0, so this should be v2 compatible.


AFAIK, there's no default alias for New-Object, and no type accelerator for Net.Sockets.TcpClient, so they can't be any shorter. You can merge the constructor and connect code into one line:

(New-Object Net.Sockets.TcpClient -A 192.168.5.5,445)

But if it can't connect, it now throws an exception, which you can't silence with -ErrorAction. So handling that ends up being 70 characters, only 8 shorter than your original after shrinking the variable name:

!!$(try{(new-object net.sockets.tcpclient -A 192.168.5.5,445)}catch{})

!! forcing the result to a bool.