When I started to learn PowerShell, I said to my self “I’m going to do everything in PowerShell”. In the beginning, the simpler stuff like unlocking a locked out account, or resetting a password, took me 5 minutes. I had to google how to do it on PowerShell, understand what I’m reading and testing it before I’m deleting all organization users.
With the practice and learning, things become much better and most important, faster. I sometimes even “race” with my friends to show them how things much more simpler and faster with PowerShell. Yet, I always found myself going back to CMD for the most basic things. while you can run native CMD commands in PowerShell, you can’t really leverage the info you get.
I made a list of the CMD commands I use the most:
- ipconfig /flushdns
Luckily for me (and for you), The Powershell team did a great job, and I needed to learn only one cmdlet for the first three command. Let me introduce you Test-NetConnection. This cmdlet will let us test ports connectivity, ping and traceroute.
The most straightforward use of this command is just Test-NetConnection. It will send a ping to a default server, see if you have an internet connection:
PS C:\> Test-NetConnection ComputerName : internetbeacon.msedge.net RemoteAddress : 188.8.131.52 InterfaceAlias : Wi-Fi SourceAddress : 10.91.8.20 PingSucceeded : True PingReplyDetails (RTT) : 5 ms
To test a specific computer or address you can use the -RemoteAddress or -ComputerName parameter:
PS C:\> Test-NetConnection -RemoteAddress www.saggiehaim.net
To perform port connectivity with Test-NetConnection, all we need to do is add the -port parameter and port number:
PS C:\> Test-NetConnection -RemoteAddress www.saggiehaim.net -Port 80 ComputerName : www.saggiehaim.net RemoteAddress : 184.108.40.206 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 10.91.8.20 TcpTestSucceeded : True
we are looking for is the “TcpTestSucceeded,” in this case, the port is open.
Same as the previous, same command, different parameter. This time we will use the -TraceRoute parameter
PS C:\> Test-NetConnection -TraceRoute www.saggiehaim.net ComputerName : www.saggiehaim.net RemoteAddress : 220.127.116.11 InterfaceAlias : Wi-Fi SourceAddress : 192.168.1.106 PingSucceeded : True PingReplyDetails (RTT) : 12 ms TraceRoute : 192.168.1.1 18.104.22.168 10.250.1.14 22.214.171.124 126.96.36.199 188.8.131.52 184.108.40.206
To get the IP address, subnet mask and default gateway for each network adapter in the machine, we can use one of the following two cmdlets:
PS C:\> Get-NetIPConfiguration InterfaceAlias : Local Area Connection InterfaceIndex : 18 InterfaceDescription : Realtek PCIe GBE Family Controller NetProfile.Name : Network 6 IPv4Address : 192.168.1.101 IPv6DefaultGateway : fe80::b88f:6eff:fe80:7e6e IPv4DefaultGateway : 192.168.1.1 DNSServer : 192.168.1.1 0.0.0.0
PS C:\> Get-NetIPAddress IPAddress : fe80::1de1:5d1f:2818:48cc%18 InterfaceIndex : 18 InterfaceAlias : Local Area Connection AddressFamily : IPv6 Type : Unicast PrefixLength : 64 PrefixOrigin : WellKnown SuffixOrigin : Link AddressState : Preferred ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore IPAddress : 192.168.1.101 InterfaceIndex : 18 InterfaceAlias : Local Area Connection AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Dhcp SuffixOrigin : Dhcp AddressState : Preferred ValidLifetime : 17:04:15 PreferredLifetime : 17:04:15 SkipAsSource : False PolicyStore : ActiveStore
Many of us IT pro’s, use the nslookup a lot. So we can do it on Powershell with the Resolve-DnsName cmdlet:
PS C:\> Resolve-DnsName www.saggiehaim.net Name Type TTL Section IPAddress ---- ---- --- ------- --------- www.saggiehaim.net AAAA 300 Answer 2400:cb00:2048:1::681f:5df5 www.saggiehaim.net AAAA 300 Answer 2400:cb00:2048:1::681f:5cf5 www.saggiehaim.net A 300 Answer 220.127.116.11 www.saggiehaim.net A 300 Answer 18.104.22.168
you want to query a specific DNS server, we can use the -Server parameter to do it:
PS C:\> Resolve-DnsName www.saggiehaim.net -Server 22.214.171.124
Before we delete all the DNS client cache, we can view the cache list to see if it’s needed
PS C:\> Get-DnsClientCache
If the list is big, we can look for the particular hostname with the -Name or -Entry parameters or particular IP address with the -Data Parameter
PS C:\> Get-DnsClientCache -Entry www.saggiehaim.net Entry RecordName Record Status Section TimeTo Data Data Type Live Length ----- ---------- ------ ------ ------- ------ ------ ---- www.saggiehaim.net www.saggiehaim.net A Success Answer 272 4 126.96.36.199 www.saggiehaim.net www.saggiehaim.net A Success Answer 272 4 188.8.131.52 www.saggiehaim.net www.saggiehaim.net A Success Answer 272 4 184.108.40.206 www.saggiehaim.net www.saggiehaim.net A Success Answer 272 4 220.127.116.11
PS C:\> Get-DnsClientCache -Data 18.104.22.168 Entry RecordName Record Status Section TimeTo Data Data Type Live Length ----- ---------- ------ ------ ------- ------ ------ ---- www.saggiehaim.net www.saggiehaim.net A Success Answer 33 4 22.214.171.124 www.saggiehaim.net www.saggiehaim.net A Success Answer 33 4 126.96.36.199 www.saggiehaim.net www.saggiehaim.net A Success Answer 33 4 188.8.131.52 [/code] To clear the client DNS cache, we can run the Clear-DnsClientCache. [code lang="Powershell"] Clear-DnsClientCache
Why Powershell Cmdlets?
So yes, we can do it with Powershell cmdlets, but if we can also run the native CMD commands inside Powershell, What is the difference? The truth is that there are no differences if you run it once, manually. The real benefits of using the Powershell cmdlets is when you run them in scripts, or doing automation tasks. When you use the CMD commands, your output is plain text a String. This means that if you want to take specific info from the command, you will have to do a lot of strings manipulations that won’t be useful for 100% of the cases.
Using the Powershell cmdlets will return Powershell Objects, Objects that contain all the information in properties we can query and pass to other cmdlets or functions, or even other scripts.
The power of Powershell objects
Using the Powershell cmdlet, We can extract or use specific property. Not all properties are showing by default and its worth the time exploring them. we can pipeline the command to get-command cmdlet to see the object information
We can choose the properties that interesting to us, for example, I want to see only the interface that performed the ping, and if it’s succeeded or not:
PS C:\> Test-NetConnection -ComputerName www.saggiehaim.net | select InterfaceAlias,PingSucceeded InterfaceAlias PingSucceeded -------------- ------------- Wi-Fi True
Simple as that 🙂 Go out there, give it a try, and remember, practice, practice, practice. It’s tough to replace old habits, but it worth the work
Hope I helped you take a step closer ditching the CMD for good.