Welcome to the next post in my Next-Level Scripting series where I show you the PowerShell scripting techniques that take your scripts to the next level - a level defined by concise, well-written code that conforms to best practices and uses the built-in tools in PowerShell whenever possible to handle common scripting tasks.
What You Don’t Know
One of the most important skills to have as a Next-Level Scripter is the ability to find the answers to what you don't know. What don't you know exactly? A lot! I've been writing PowerShell scripts for years and I still regularly use the techniques I will outline in this post to discover information that I don't know. And I think it's safe to say that I will never stop using these techniques because there is just too much "stuff" to know it all. And there’s nothing wrong with that so please don’t ever think that your scripting skills are lacking if you regularly have to look things up!
Warning – Mild Developer-Speak Ahead
I have to apologize but it's time for some developer-speak. Don't worry though, there are only two main terms you need to understand as your foundation - methods and properties.
To explain these terms, let's take a step back from the world of computers and look at a real-world example to which pretty much everyone can relate to - the car. In developer-speak the car is known as an object. Every car (object) has, among other things, a colour, weight and model. These are fixed attributes that you can't change at will and are what as known as properties. In developer-speak, the colour is a property of the car object.
But, there are also things that you can change and to make these changes you need to actually do something. For example, the car can accelerate but it doesn't just do that by itself; you need to press the gas pedal to make it accelerate. In the world of objects this is what is known as a Method. Once again, to put it in developer speak: you use the accelerate method to make the car object accelerate.
And that's all you really need to know about objects, methods and properties for the purposes of this post! See, it wasn't too bad was it?
Exploring Objects
Remember the car object from our example above? When you work with PowerShell you are working with objects as well and all of these objects have methods and properties. The question though when you're dealing with these objects is, "What can I do with this object via its methods and properties?" Like I said in the introduction to this post, there is absolutely no way you can know what all the properties and methods are for everything you'll ever come across and you therefore must know how to find this information out.
Enter Get-Member…this cmdlet is what shows you the methods and properties of an object. To get it to tell you this information you need to tell it what object you're interested in. Let's look at a simple example to show how this is done.
You've been asked to write a script to find the date created for every subfolder and file of a given folder. You've never done this before but suspect that there is a property which contains the creation date of a folder/file. Your mission then is to find this property (I know you could easily look this up on on the internet but if you want to truly learn PowerShell you need to figure out how to do these things yourself - it's the best way to learn).
You know about the Get-ChildItem cmdlet so you decide that's a good place to start. You run it against your folder of interest and examine the output.
Well that's a little disappointing - no mention of creation date, only modified date. But does this mean you should throw your monitor at the wall and go home weeping? No! In most cases, there is way more information you can retrieve and PowerShell is only showing what it has been programmed to show you by default.
As you've probably figured by now, this is one of those times where you need to run Get-Member. Remember how I said above that you must tell Get-Member what object you're interested in? That object will come from Get-ChildItem via this command:
| 001 | Get-ChildItem C:\CorpMiscellaneous | Get-Member |
And here’s the output:
This command will retrieve all the available Methods and Properties of the C:\CorpMiscellaneous folder. Since we're trying to retrieve a property and not a method, we can ignore all the methods in the list and focus only on the properties, highlighted in red. And lo and behold, what is the 2nd property we see in the list, highlighted in green? CreationTime! Pleased with this discovery, you decide that you want to report on the name of the file and the creation time as well as sort the results by the creation time. Let's try this out:
| 001 | Get-ChildItem C:\CorpMiscellaneous | Select-Object Name, CreationTime | Sort-Object CreationTime |
And here’s the output:
In this example, we used the Select-Object cmdlet to specify the properties we want to retrieve from each object, in this case Name and CreationTime (you could have specified any valid property in this command). We then pipe the results to Sort-Object and tell it to sort the results based on CreationTime. If you wanted to save the results to a file you could also add Export-CSV at the end of the command.
And that's all there is to it. You can use this approach for any object to discover what you can do with it and in doing so, take the next step on your way to becoming a Next-Level Scripter.
No comments:
Post a Comment