Ditch CMD for Powershell

PowerShell

Written by:

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, and I will show that soon.

I made a list of the CMD commands I use the most:

  • ping
  • telnet
  • tracert
  • ipconfig
  • nslookup
  • 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.
Replacing Ping:
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:

To test a specific computer or address you can use the -RemoteAddress or -ComputerName parameter:

Replacing TelNet:
To perform port connectivity with Test-NetConnection, all we need to do is add the -port parameter and port number: What we are looking for is the “TcpTestSucceeded,” in this case, the port is open.
Replacing Tracert:
Same as the previous, same command, different parameter. This time we will use the -TraceRoute parameter

Replacing IPconfig
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:

Replacing NSlookup
Many of us IT pro’s, use the nslookup a lot. So we can do it on Powershell with the Resolve-DnsName cmdlet:

If you want to query a specific DNS server, we can use the -Server parameter to do it:
ipconfig /flushdns
Before we delete all the DNS client cache, we can view the cache list to see if it’s needed

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

 

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.
Powershell IPconfig get member
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 PsObject 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:

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.

Comments are closed.