Introduction to PowerShell - part 2 - Pipeline, Scripts and Syntax

by Dmitry Kirsanov 25. January 2012 01:30

Introduction to PowerShellAll of a sudden I decided to spend one hour of this evening to make the second part of PowerShell introduction for Windows system administrators. I just noticed, that the previous part was made in… November, and having quite a few requests to continue I just couldn’t resist! So, this is pure improvisation, although I tried to make it as smooth as possible.

This time we’ll dig further into what system administrators do most – working with large arrays of data, such as files, ACL lists and finding objects by special parameters. You will learn about variables in PowerShell and how you can effectively use them.

Objects in PowerShell

As it was discussed in the first part, what you get as the result of your query is an object, and you only see those properties which you request to be displayed. It doesn’t mean, though, that other properties are omitted and not retrieved. As long as you have the handle to that object, you can retrieve additional information.

Like every object in the world around you, objects in PowerShell have two important things – properties and methods. Property is a piece of information about the object, like the color, text, length or width. The Method is some action – something you can order this object to do. Like – if you would have an object representing the file, it would be logical to expect such properties as File Name, File Size and dates of creation and last change. And it would support methods to rename, delete, copy or move that objects.

So, in short, working with objects (in your first scripts, that is) usually looks like this: you get all objects, you filter out the ones you don’t need, and then – you perform an action on the rest. For example, if you want to delete the old files in the directory, then first you select all files in that directory, then you specify (with where clause) that you only need those which are older than 3 months, and then you call the Delete method.

See the video I made this evening about using the pipeline, filtering out the arrays of objects and finding the ones you need using the custom criteria, such as similarities and equations.

Introduction to PowerShell, part 2 - Pipeline, Scripts and Syntax

Each object belongs to a single type. For example, object John is of type Human. A type, on the other hand, could inherited from another type. For example, the type Human inherits from the type mammal, which inherits from the type animal and so on.

Each type describes what properties and methods the object supports. For example, the type Human describes, that each object of that type has such properties as height, weight, eye color and eyes themselves, as well as name and whatever else you can recall the humans have.
So you can create (or find) the object of that type and query it for his properties. That way you could get the names of all humans you’ve found.
Some properties can be changed, some can’t. For example, you could change the name of the human object, but you can’t change it’s height. It could also have some internal logic – like you can’t decrease it’s weight to less than 40 kilos. In that case – even though you could set the property, it doesn’t mean it will be set, you could have an error instead. A real world example of such logic is the data type – when the property is of the data type byte, you couldn’t set it to the value lower than 0 or higher than 255, as these are the boundaries of the byte.

The property could be marked as get, get-set or even just set. Get means you can read the property, get with set means you can also write to that property (i.e. change it), and if on some rare occasion you’ll see the set only property – it means you can set the value, but you can’t retrieve it later. You could find such in some security objects, like the password field.

Methods are actions you could have with that objects. For the objects of the type Human, such action could be the handshake, or speak, walk, sleep, eat. Some of such methods could be inherited from the Mammals type, some could be unique.

Methods may or may not return any result.

The back tick character

The ` character (“back tick”) is used to type your command in more than one line. Unlike the | (pipeline) character, it can split the same expression. The pipeline character splits expressions, allowing you to type more than one of them in one context.

What’s in the next part

Make sure you subscribe to this blog or the YouTube channel (or Twitter) in order to be notified when the next part is available. As I am currently in the process of making a few walkthroughs, the priority will be given to the ones with more likes and views, to satisfy the most active visitors first.

The next part of the PowerShell walkthrough will be devoted to variables and data types, variable Scopes, and arrays. Overall I am planning to create 10-12 episodes, with last ones devoted to corporate system administration using PowerShell and SCCM/SCSM platforms.

But anyway, in order to explain the hardcore part we should make you a pro in PowerShell first, so stay tuned!

blog comments powered by Disqus