{"id":754,"date":"2010-07-29T15:24:37","date_gmt":"2010-07-29T19:24:37","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=754"},"modified":"2010-07-29T15:24:37","modified_gmt":"2010-07-29T19:24:37","slug":"get-your-tcp-ports-here","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/","title":{"rendered":"Get Your TCP Ports Here!"},"content":{"rendered":"<p>Once again, the PowerShell forum at <a href=\"http:\/\/www.scriptinganswers.com\" target=\"_blank\">ScriptingAnswers.com<\/a> has undone me. I was answering a question about running Netstat in a PowerShell session on a remote computer which got me thinking about a PowerShell function to turn NETSTAT.EXE output into objects. Once you have an object then you can do all sorts of things.\u00a0 Needless to say I got hooked on working something up instead of working on what I had planned for the day. The good news is that you get a tool and there's still a few hours of the day left for me to get something else accomplished. I'm sure there are plenty of variations on this topic already out there, but here's my contribution: Get-TCP.<br \/>\n[cc lang=\"Powershell\"]<br \/>\nFunction Get-TCP {<br \/>\n<#\n.Synopsis\nGet TCP Netstat information\n.Description\nThis function calls the command line NETSTAT.EXE tool and returns an object representation\nof the TCP data.\n\nProtocol\u00a0\u00a0 : TCP\nLocalhost\u00a0 : 172.16.10.127\nLocalPort\u00a0 : 49259\nRemoteHost : 74.201.86.29\nRemotePort : https\nStatus\u00a0\u00a0\u00a0\u00a0 : ESTABLISHED\n\nThe default is the local computer without name resolution. However you can specify a remote\ncomputername, assuming the remote computer is running PowerShell 2.0 and has remoting enabled.\nUse -ResolveHost to resolve IP addresses to host names. This is a little slower.\n\nThis function will only return IPv4 hosts and addresses\n.Parameter Computername\nThe name of the computer to query. The default is the localhost.\n.Parameter ResolveHost\nResolve IP addresses to host names. This is a little slower. The is the equivalent of running\nNetstat.exe without any parameters.\n.Parameter ResolvePort\nResolve the service name associated with the port. This requires access to the legacy\nServices file found at $env:windir\\system32\\drivers\\etc\\services.\n.Parameter IncludeRaw\nInclude the raw netstat data.\n.Example\nPS C:\\> get-tcp Return TCP Netstat information for the local computer. .Example PS C:\\&gt; get-tcp \"Server1\",\"Server2\" -resolve<\/p>\n<p>Return TCP Netstat information for computers Server1 and Server2 with resolved IP addresses.<br \/>\n.Example<br \/>\nPS C:\\> get-content computers.txt } get-tcp -resolveHost | where {$_.RemotePort -eq 80} | format-table -autosize<\/p>\n<p>Get HTTP connections for every computer in computers.txt and present as a formatted table.<br \/>\n.Example<br \/>\nPS C:\\> get-tcp | sort RemotePort | Select RemotePort -unique<\/p>\n<p>Get a sorted list of all remote connections by port.<br \/>\n.Inputs<br \/>\nStrings<br \/>\n.Outputs<br \/>\nCustom object<br \/>\n.Link<br \/>\nhttp:\/\/jdhitsolutions.com\/blog<\/p>\n<p>.Link<br \/>\nInvoke-Command<\/p>\n<p>.Notes<br \/>\nNAME:\u00a0\u00a0\u00a0\u00a0\u00a0 Get-TCP<br \/>\nVERSION:\u00a0\u00a0 1.5<br \/>\nAUTHOR:\u00a0\u00a0\u00a0 Jeffery Hicks<br \/>\nLASTEDIT:\u00a0 July 29, 2010<\/p>\n<p>Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010)<\/p>\n<p>#><\/p>\n<p>[cmdletbinding()]<\/p>\n<p>Param(<br \/>\n[Parameter(Position=0,ValueFromPipeline=$True)]<br \/>\n[string[]]$Computername=$env:computername,<br \/>\n[switch]$ResolveHost,<br \/>\n[switch]$ResolvePort,<br \/>\n[switch]$IncludeRaw<br \/>\n)<\/p>\n<p>Begin {<br \/>\nWrite-Verbose \"Starting $($myinvocation.mycommand)\"<br \/>\nif ($ResolveHost) { Write-Verbose \"Resolving host names\"}<br \/>\nif ($ResolvePort) {Write-Verbose \"Resolving port names\"}<br \/>\n#put everything into a script block so that if the computer<br \/>\n#is remote it can be executed using Invoke-Command<br \/>\n$scriptblock={netstat.exe -n | where {$_.Contains(\"TCP\") -AND $_ -notmatch \"\\[\"}}<\/p>\n<p>Write-Verbose \"Caching Services information\"<br \/>\n$file=\"$env:windir\\system32\\drivers\\etc\\services\"<br \/>\n#get just tcp services<br \/>\n$services=Get-Content -Path $file | Select-String -Pattern \"\/tcp\"<\/p>\n<p>Write-Verbose $($scriptblock.ToString())<\/p>\n<p>#define the name resolution function<br \/>\nFunction Resolve-DNS\u00a0 {<\/p>\n<p>[cmdletbinding()]<\/p>\n<p>Param(<br \/>\n[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,<br \/>\nHelpMessage=\"Enter an IP Address to resolve\")]<br \/>\n[string[]]$Address<br \/>\n)<br \/>\nBegin {<br \/>\nWrite-Verbose \"Starting Resolve-DNS\"<br \/>\n#turn off error pipeline<br \/>\n$errorActionPreference=\"SilentlyContinue\"<br \/>\n}<br \/>\nProcess {<br \/>\nforeach ($IP in $Address) {<br \/>\nWrite-Verbose \"Resolving $IP\"<br \/>\n$dns=[System.net.DNS]::GetHostByAddress(\"$IP\")<br \/>\nif (-not $dns) {<br \/>\nWrite-Verbose \"no record found for $IP\"<br \/>\n$h=$IP<br \/>\n$a=$null<br \/>\n}<br \/>\nelse {<br \/>\n$h=$dns.hostname<br \/>\n$a=$dns.AddressList<br \/>\n}<br \/>\nNew-Object -TypeName PSobject -Property @{<br \/>\nHostname=$h<br \/>\nAddresses=$a<br \/>\n}<br \/>\n} #foreach<br \/>\n} #process<\/p>\n<p>End {<br \/>\n#turn On error pipeline<br \/>\n$errorActionPreference=\"Continue\"<br \/>\n}<br \/>\n} #end function<\/p>\n<p>} #begin<\/p>\n<p>Process {<br \/>\nforeach ($computer in $computername) {<br \/>\nWrite-Verbose \"Getting raw NETSTAT data from $($computer.toUpper())\"<br \/>\nif ($computer -eq $env:computername) {<br \/>\n#just run the script block if local computer<br \/>\n$data=&amp;$scriptblock<br \/>\n}<br \/>\nelse {<br \/>\nTry {<br \/>\n$data=Invoke-Command -ScriptBlock $scriptblock -computername $computer -errorAction \"Stop\"<br \/>\n}<br \/>\nCatch {<br \/>\nWrite-Warning \"Failed to run command on $computer\"<br \/>\nWrite-Warning $error[0].exception.message<br \/>\n}<br \/>\n} #else<br \/>\n#process data<br \/>\nWrite-Verbose \"Returned $($data.count) items\"<br \/>\nWrite-Verbose \"Parsing data\"<br \/>\n#initialize a hash table for IP names<br \/>\n$hash=@{}<br \/>\nforeach ($tcp in $data) {<br \/>\nWrite-Verbose $tcp.trim()<br \/>\n#split the line removing empty spaces<br \/>\n$arr=$tcp.trim().split() | where {$_}<br \/>\n$localData=$arr[1].Split(\":\")<br \/>\n$remoteData=$arr[2].Split(\":\")<br \/>\n$protocol=$arr[0]<br \/>\n$raw=$tcp.trim()<br \/>\n$local=$localData[0]<br \/>\n[int]$localPort=$localData[1]<br \/>\n$remote=$remoteData[0]<br \/>\n[int]$remotePort=$remoteData[1]<br \/>\n$status=$arr[3]<\/p>\n<p>#if resolve host requested build a dictionary or<br \/>\n#ip addresses so we don't need to resolve one already resolved<br \/>\n#and change the property value accordingly<br \/>\nif ($resolveHost) {<br \/>\nif ($hash.contains(\"$local\")) {<br \/>\n$local=$hash.Item(\"$local\")<br \/>\n}<br \/>\nelse {<br \/>\n#look up name<br \/>\nWrite-Verbose \"Resolving $local\"<br \/>\n$localLookup=Resolve-DNS \"$local\"<br \/>\n#add to hash<br \/>\n$hash.Add(\"$local\",$localLookup.Hostname)<br \/>\n#update property<br \/>\n$local=$localLookup.Hostname<br \/>\n}<br \/>\n#do the same for remote<br \/>\nif ($hash.contains(\"$remote\")) {<br \/>\n$remote=$hash.Item(\"$remote\")<br \/>\n}<br \/>\nelse {<br \/>\n#look up name<br \/>\nWrite-Verbose \"Resolving $remote\"<br \/>\n$remoteLookup=Resolve-DNS \"$remote\"<br \/>\n#add to hash<br \/>\n$hash.Add(\"$remote\",$localLookup.Hostname)<br \/>\n#update property<br \/>\n$remote=$RemoteLookup.Hostname<br \/>\n}<br \/>\n} #if $resolveHost<\/p>\n<p>#resolve ports if specified<br \/>\nif ($resolvePort) {<br \/>\n#this is a mini scriptblock to return the service name<br \/>\n#in lieu of a full-blown function<br \/>\n$getsvc={Param ([string]$port)<br \/>\nWrite-Verbose \"searching for $port\"<br \/>\n$service=$services | Select-String -pattern\u00a0 \"\\s$port\/tcp\"<\/p>\n<p>if ($service) {<br \/>\n$data=$service.ToString().Trim().split() | where {$_}<br \/>\nWrite-Output $data[0]<br \/>\n}<br \/>\nelse {<br \/>\nWrite-Output $port<br \/>\n}<\/p>\n<p>} #end $getSvc<\/p>\n<p>[string]$localPort=&amp;$getsvc $localPort<br \/>\n[string]$remotePort=&amp;$getsvc $remotePort<\/p>\n<p>} #end resolveport<\/p>\n<p>#create a new object and pipe it to Select-Object so that<br \/>\n#the properties are in a nice order<br \/>\n$obj=New-Object -TypeName \"PSObject\" -Property @{<br \/>\nProtocol=$protocol<br \/>\nLocalhost=$local<br \/>\nLocalPort=$localPort<br \/>\nRemoteHost=$remote<br \/>\nRemotePort=$remotePort<br \/>\nStatus=$status<br \/>\n}<br \/>\nif ($IncludeRaw) {<br \/>\n$obj | Add-Member -MemberType \"Noteproperty\" -Name \"Raw\" -Value $raw -PassThru |<br \/>\nSelect-Object -Property Protocol,LocalHost,LocalPort,RemoteHost,RemotePort,Status,Raw<br \/>\n}<br \/>\nelse {<br \/>\n$obj | Select-Object -Property Protocol,LocalHost,LocalPort,RemoteHost,RemotePort,Status<br \/>\n}<br \/>\n} #foreach $tcp<br \/>\n} #foreach computer<br \/>\n} #Process<\/p>\n<p>End {<br \/>\nWrite-Verbose \"Ending $($myinvocation.mycommand)\"<br \/>\n}<\/p>\n<p>} #end function[\/cc]<br \/>\nThis is an advanced Windows PowerShell 2.0 function. By default it converts output from Netstat -n to object. I've parsed the output so you get an object like this for each line.<\/p>\n<p>[cc lang=DOS]<br \/>\nProtocol\u00a0\u00a0 : TCP<br \/>\nLocalhost\u00a0 : 172.16.10.122<br \/>\nLocalPort\u00a0 : 60553<br \/>\nRemoteHost : 74.201.86.29<br \/>\nRemotePort : 443<br \/>\nStatus\u00a0\u00a0\u00a0\u00a0 : ESTABLISHED[\/cc]<\/p>\n<p>When you run Netstat without any parameters it resolves both host and port names. I've separated the two so you can use either -ResolveHost and\/or -ResolvePort. The former uses an embedded function to get the host entry name by IP address. If not found, then the IP address is used. This function by the way only returns IPv4 information. Service ports are resolved by finding the associated TCP port in the legacy Services file. What you end up with is something like this:<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-782\" title=\"get-tcp\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png\" alt=\"\" width=\"300\" height=\"171\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp.png 1005w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>The function can also accept a remote computername as it uses Invoke-Command to run the Netstat command remotely.\u00a0 All the results are then processed locally. You can pipe computernames to the function or it accepts arrays.<\/p>\n<p>If you have any questions on the nitty-gritty details, please post a comment.<\/p>\n<p>Download <a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/Get-TCP.txt\">Get-TCP.ps1<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again, the PowerShell forum at ScriptingAnswers.com has undone me. I was answering a question about running Netstat in a PowerShell session on a remote computer which got me thinking about a PowerShell function to turn NETSTAT.EXE output into objects. Once you have an object then you can do all sorts of things.\u00a0 Needless to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72,75,99,135],"tags":[32,122,213,534,214],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-commandline","category-powershell-v2-0","category-windows-7","category-windows-server","tag-functions","tag-invoke-command","tag-netstat","tag-powershell","tag-remote"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Get Your TCP Ports Here! &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get Your TCP Ports Here! &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Once again, the PowerShell forum at ScriptingAnswers.com has undone me. I was answering a question about running Netstat in a PowerShell session on a remote computer which got me thinking about a PowerShell function to turn NETSTAT.EXE output into objects. Once you have an object then you can do all sorts of things.\u00a0 Needless to...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2010-07-29T19:24:37+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Get Your TCP Ports Here!\",\"datePublished\":\"2010-07-29T19:24:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/\"},\"wordCount\":947,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2010\\\/07\\\/get-tcp-300x171.png\",\"keywords\":[\"functions\",\"Invoke-Command\",\"Netstat\",\"PowerShell\",\"Remote\"],\"articleSection\":[\"CommandLine\",\"PowerShell v2.0\",\"Windows 7\",\"Windows Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/\",\"name\":\"Get Your TCP Ports Here! &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2010\\\/07\\\/get-tcp-300x171.png\",\"datePublished\":\"2010-07-29T19:24:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2010\\\/07\\\/get-tcp.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2010\\\/07\\\/get-tcp.png\",\"width\":\"1005\",\"height\":\"574\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/commandline\\\/754\\\/get-your-tcp-ports-here\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"CommandLine\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/commandline\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get Your TCP Ports Here!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get Your TCP Ports Here! &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/","og_locale":"en_US","og_type":"article","og_title":"Get Your TCP Ports Here! &#8226; The Lonely Administrator","og_description":"Once again, the PowerShell forum at ScriptingAnswers.com has undone me. I was answering a question about running Netstat in a PowerShell session on a remote computer which got me thinking about a PowerShell function to turn NETSTAT.EXE output into objects. Once you have an object then you can do all sorts of things.\u00a0 Needless to...","og_url":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/","og_site_name":"The Lonely Administrator","article_published_time":"2010-07-29T19:24:37+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Get Your TCP Ports Here!","datePublished":"2010-07-29T19:24:37+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/"},"wordCount":947,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png","keywords":["functions","Invoke-Command","Netstat","PowerShell","Remote"],"articleSection":["CommandLine","PowerShell v2.0","Windows 7","Windows Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/","url":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/","name":"Get Your TCP Ports Here! &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp-300x171.png","datePublished":"2010-07-29T19:24:37+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/07\/get-tcp.png","width":"1005","height":"574"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/commandline\/754\/get-your-tcp-ports-here\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"CommandLine","item":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},{"@type":"ListItem","position":2,"name":"Get Your TCP Ports Here!"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1483,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1483\/test-port-2-0\/","url_meta":{"origin":754,"position":0},"title":"Test Port 2.0","author":"Jeffery Hicks","date":"May 31, 2011","format":false,"excerpt":"A few years ago I updated a PowerShell script I came across to scan a computer for open ports. My initial revision was aimed at making it more pipeline friendly in PowerShell v1.0. I recently needed to use the function for a project and realized it could benefit from a\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/05\/test-port-300x126.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3673,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3673\/convert-text-to-object-with-powershell-and-regular-expressions\/","url_meta":{"origin":754,"position":1},"title":"Convert Text to Object with PowerShell and Regular Expressions","author":"Jeffery Hicks","date":"February 10, 2014","format":false,"excerpt":"A few weeks ago I was getting more familiar with named captures in regular expressions. With a named capture, you can give your matches meaningful names which makes it easier to access specific captures. The capture is done by prefixing your regular expression pattern with a name. PS C:\\> \"UNC\u2026","rel":"","context":"In &quot;CommandLine&quot;","block_context":{"text":"CommandLine","link":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},"img":{"alt_text":"squarepattern","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/squarepattern-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":7978,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7978\/deploy-openssh-to-windows-server\/","url_meta":{"origin":754,"position":2},"title":"Deploy OpenSSH to Windows Server","author":"Jeffery Hicks","date":"December 17, 2020","format":false,"excerpt":"Yesterday I shared some PowerShell ideas for remotely setting up the server component of ssh on a remote Windows 10 system. This would allow you to establish an ssh session to the desktop or even use PowerShell remoting over ssh. Next, we need to look at doing the same thing\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/12\/win2019-psssh.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7447,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7447\/get-installed-powershell-versions\/","url_meta":{"origin":754,"position":3},"title":"Get Installed PowerShell Versions","author":"Jeffery Hicks","date":"May 7, 2020","format":false,"excerpt":"As is the norm for a typical day, I was working on one thing when I was distracted by a shiny rabbit hole (to mix some metaphors). Half a day later I have a new PowerShell function that not only might you find useful, but I think it has some\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/get-psinstalled-result2.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7458,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7458\/a-powershell-remote-function-framework\/","url_meta":{"origin":754,"position":4},"title":"A PowerShell Remote Function Framework","author":"Jeffery Hicks","date":"May 8, 2020","format":false,"excerpt":"The other day I shared a PowerShell function to query the registry on remote computers to find installed versions of PowerShell. The function leveraged PowerShell remoting with the flexibility of using a computer name with an optional credential or existing PSSessions. The more I thought about it, the more I\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/stop-remoteprocess.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":8499,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8499\/using-the-powershell-ise-as-a-remote-management-console\/","url_meta":{"origin":754,"position":5},"title":"Using the PowerShell ISE as a Remote Management Console","author":"Jeffery Hicks","date":"July 20, 2021","format":false,"excerpt":"Way back before the days of PowerShell Core, or even VS Code for that matter, the PowerShell ISE was the center of my PowerShell world. I spent a lot of time finding ways to make it easier for me to use and to push it to its limits. Naturally, the\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/ise-remotetab-form2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}