Using Windows Server 2008 Powershell to perform common network commands


There are things we do every day while administering our Windows network but if we were asked to do them from the command line, unlike most Linux admins, most of us Windows admins would struggle. Windows has always been weak when it comes to command line tools. In my opinion, that has changed with the advent of Windows Powershell. Powershell (or PS as it is called) can do so many things that could not be done before. In this article, we will focus on how Powershell can help you perform some common networking functions from the command line. Read on to find out more!


What is Powershell?


Powershell is an installable Feature of Windows Server 2008. To Install Powershell, you must install the Powershell Feature in the Add Features Wizard. It only takes a minute or so to install and once it is installed, you have access to an amazing command line scripting language. Unlike other scripting languages in Windows, Powershell is designed just for us system administrators. Powershell uses .NET and utilizes “cmdlets” (or “command-lets”) to do its job. As a PS user, you can use the cmdlets by themselves or you can connect them together to perform much more powerful tasks.


Once you have PS installed, you should be able to go to Start -> All Programs -> Windows Powershell 1.0, and click on Windows PowerShell. At that point, you should see a blue CLI window come up that looks like this:



Figure 1:
Windows Powershell Command Window


You can always tell you are in Powershell because of the “PS” at the front of each prompt, like this:


PS C:\Users\Administrators


Now that you have powershell installed and have the command window up, let me show you some common networking tasks that you would perform with PS.


List IP addresses on your Server


To list the IP addresses on your Windows 2008 Server, you would use this command line string:


Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property IPAddress


Here is what the output looks like on my Windows 2008 Server:



Figure 2:
Listing IP address with Windows Powershell


As you can see, from this, the output shows us that I have one adaptor with an IP address V4 address and an IP V6 address on this Windows 2008 Server. That, in itself, is not that amazing, but think what you could do with this when combined with other scripting functions.


As the User Guide for powershell teaches us, the output is an array and you can see the IP address only by directing the output to “Select-Object” (after disabling IPV6), like this:


Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -ExpandProperty IPAddress



Figure 3:
Listing IP address ONLY with Windows Powershell


List your network adaptor configuration with Powershell


To show the basic configuration of your network adaptor, you can use the following command:


Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE –ComputerName .


While this article focuses on using PS in Windows Server 2008, it can also be used in Windows XP, Vista, or Server 2003. In fact, here is sample output of the command above, done on my Windows XP workstation:



Figure 4:
PowerShell showing network adaptor configuration in Windows XP


Pinging a computer with PowerShell


While the PS command line can still perform all the regular Windows commands (like ping), the power of Powershell is that you can take that output and easily modify it.


Here is an example of how that can be done, courtesy of the Windows Powershell user guide. In this example, the output of the Win32_PingStatus is parsed using Select-Object. In this case, the output just shows the response time & status code.


Here is the command that was used:


Get-WmiObject -Class Win32_PingStatus -Filter “Address=’127.0.0.1′” -ComputerName . | Select-Object -Property Address,ResponseTime,StatusCode


And here is the output on my Windows machine:



Figure 5:
PowerShell output showing ping status parsed with Select-Object


Sharing a Folder with Windows Powershell


I am going to be honest with you. Using Powershell commands are not always as easy as existing Windows commands that you are already familiar with. Here is an example.


The following PS command will share a folder at C:\temp as “davidtemp” and place a nice command on it:


(Get-WmiObject -List -ComputerName . | Where-Object -FilterScript {$_.Name -eq “Win32_Share”}).InvokeMethod(“Create”,(“C:\temp”,”davidtemp”,0,25,”David’s Temp Folder”))


On the other hand, you could just use the tried and trusted net share command, like this:


net share davidtemp=C:\temp /remark:”David’s Temp Shared Folder”



Figure 6:
PowerShell output vs. traditional net share command


Notice how the Powershell command did not even work and gave me a nasty looking error. I tried it on Windows XP and Vista and I was the administrator of the system.


On the other hand, the net share command was simple and worked the first time I tried it. So, while I am here to tell you all that you can do with Powershell, do not forget that Powershell can run all your traditional Windows networking commands and in many cases they will be easier to use. However, you will get to a point where those traditional commands do not do all that you need them to do and that is where Powershell will step in and save the day.


Over the Network & more Complex commands


Again, if you are only going to do the most basic network admin commands with Powershell, you will probably get frustrated and not use it anymore. That is because, like any scripting language, there is a learning curve.


However, once you get past that learning curve and learn how to use it, you get all the time-saving and efficiency benefits from the new language you have learned.


As you might have noticed in some of the previous examples, one of the parameters for most Powershell commands is the “computername”. When we put dot (“.”) as the computername, that is the local host (our computer). However, we can also substitute any IP address or Windows computer name in the domain to not only use these commands on your PC but create powerful scripts with Powershell that you can run on all computers on your network.


For example, here is a command that pulls an IP address from a Dell computer across the LAN:


Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName DELL9400


Here are the results:



Figure 7:
Powershell results from retrieving IP address of remote PC


And here is a “zoomed” graphic showing just the pertinent info:



Figure 8:
Close up of Powershell results from retrieving IP address of remote PC


So the ability to act on remote computers is a major feature of Powershell but another major feature is the ability to filter output and combine output from one command with another command.


Take a look at this example:


“127.0.0.1”,”localhost”,”research.microsoft.com” | ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter (“Address='” + $_ + “‘”) -ComputerName .} | Select-Object -Property Address,ResponseTime,StatusCode


In this example, a list was provided of IP addresses and domain names. That list was piped (sent) to “ForEach-Object”. On each of those “objects” (the IP address / domain names), the “Get-WmiObject” PingStatus was run. The output of the ping of each of those domain names was then parsed with “Select-Object” and only the address, response times, and status codes where shown.


Here is the output:



Figure 9:
Pinging using a list by combining & piping output


I think that this example shows some of the power of Powershell. As you can see you can direct and redirect input and output in all different directions to accomplish your system administrative goals.


What else do I need to know?


As I said before, please keep in mind that Powershell is not just run on Windows Server 2008 but it can also be used in Windows XP, Vista, and Server 2003. Powershell is free and quick to download as it is only about 6MB. All you have to do is go to the Microsoft Windows Powershell download site at this URL.


Summary


Windows Powershell is extremely powerful. This article cannot begin to explain all that you could do with Powershell but I hope that it gives you an idea of what is possible and inspires you to learn more about Powershell. Increasingly everyday, there are new books, classes, and tons of web pages that demonstrate the usefulness of Powershell to Windows system administrators. With Windows admins being so used to using the GUI interface, it will take time for Powershell to be adopted in companies where it is not immediately necessary. I believe that admins out there will use Powershell to create much shorter scripts that perform certain functions but can be combined with other scripts to do more complex functions. I look forward to seeing Powershell become more and more adopted in Windows shops across the world!


Special Thank you to Microsoft for their command line script examples found in the Windows Powershell User Guide documentation.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top