Weekend Scripter: Run All Cmdlets from a Module

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about running all Get cmdlets from a specific Windows PowerShell module.

Microsoft Scripting Guy, Ed Wilson, is here. Today, the Scripting Wife and I are once again on a train—this time we are heading to Warsaw to visit Windows PowerShell MVP Bartek Bielawski. Bartek won the Scripting Games two years ago and got a free pass to TechEd in Atlanta. The Scripting Wife and I met him at the Atlanta airport, and we got to spend a lot of time with him at TechEd. He is a special person and really loves Windows PowerShell.

Time on the train gives me a great chance to play around with Windows PowerShell. One of the things that has always impressed me about Windows 8 with Windows PowerShell 3.0 is the great number of cmdlets. It is safe to say that, so far, I have only scratched the surface in working with these cmdlets, and I have been using Windows PowerShell 3.0 since before it was ever publically available in beta or in community preview—there are just that many cmdlets. In particular, I was curious about all the cmdlets (functions) exposed through the NetAdapter module. Finally, I could not resist, I had to run all of them.

I admit that this is not the most useful of the Hey, Scripting Guy! Blog posts, but hey, it is the weekend after all. And, it might give you some ideas for exploring the plethora of cmdlets (functions) in Windows 8. I must also admit that the Windows PowerShell team has written a blog post that states that the Invoke-Expression cmdlet is considered “dangerous” … but, hey, once again I am playing around on my own laptop on a high-speed train as it hurls its way towards Warsaw, Poland. And what I do on a train going to Warsaw is not necessarily a good network management technique, but it can be fun.

So, with the disclaimers out of the way….

First, find all of the Get cmdlets in a module

The first thing I want to do is find all of the Get cmdlets from within a module. I do not want to call a bunch of SET cmdlets because, first of all, they might not work without specific parameters, and second of all, they MIGHT work and change a bunch of things on my laptop. So, for both reasons, I stick with the Get  cmdlets because they are the ones that expose information. Here is the command to find all of the Get cmdlets from within the NetworkAdapter module (gcm is an alias for the Get-Command cmdlets).

gcm -Module netadapter -Verb get

Run a command through the pipeline

The cool thing about the Invoke-Expression cmdlet is that it permits me to create a command on the fly, and, subsequently, to execute the command I just created. Invoke-Expression evaluates a string and will run it as a command—this is very powerful, but, at the same time, can be dangerous because it allows me to run an arbitrary command simply by passing it in a string.

What I want to do is to first find all of the Get cmdlets from the NetworkAdapter module, and then to run each of the Get cmdlets. Here is the command that accomplishes this task.

gcm -Module netadapter -Verb get | % { Invoke-expression $_.name }

This command produces a ton of information. The command and a single screen full of information are shown in the following image.

Image of command output

Because this produces so much information, and I cannot read as fast as the information scrolls past the screen, I decided to write the information to a text file. Here is the revised command.

gcm -Module netadapter -Verb get | % { Invoke-expression $_.name } >> c:\fso\netadapter.txt ; notepad c:\fso\netadapter.txt

The netadapter.txt file appears within seconds of executing the command. The text file is shown here.

Image of command output in text file

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon