This PowerShell tutorial is based on my book with the same title. You can get the book on Amazon.
There are 4 articles with a total of 7 tutorials. This is PowerShell Tutorial 7 of 7. If you have not read PowerShell tutorials 1, 2, 3 4, 5 & 6, I recommend you read them before reading these tutorials.
PowerShell Tutorial 7: PowerShell Objects
Knowledge of PowerShell Objects and their properties is essential to PowerShell scripting. Without the knowledge of object properties and how to manipulate them, you may not have a good grasps of PowerShell scripting.
In PowerShell Tutorial 7, you will learn about PowerShell objects, their properties, and how to access and manipulate them. You will also learn about object members and how to use them.
Finally, you will learn the difference between object properties and methods.
What are PowerShell Objects and Object Properties?
A PowerShell object is a collection of data that represents an item. An object is made up of three types of data: the object’s type, its methods, and its properties.
Let’s bring this definition to life by executing the command below:
Get-ChildItem D:\PS-Tutorial\websites.txt | Get-Member
Here is the result of the command in PowerShell.
Refer to the screenshot above. Beneath the command (before the columned results), there is this text:
TypeName: System.IO.FileInfo
This is the Object Type. FileInfo, represents a file. To show you two other Object Types, run the commands below:
Get-Process | Get-Member
Get-ChildItem D:\PS-Tutorial | Get-Member
The path “D:\PS-Tutorial” is a folder. You can replace this with any folder on your computer.
The screenshot below shows the results of the two commands. On the left, we have a Process Object Type. However, the right side of the screenshot displays another Object Type – DirectoryInfo.
An object’s properties store information about the object; its methods are actions you can perform on the object. In other words, objects properties store data about the object, and it’s methods let you manipulate the object.
See the next PowerShell Tutorial for details…
Object Properties and Methods
An object’s properties are like the components of the object. For instance, a file has the following properties: Name, Extension, LastWriteTime, Length, etc.
You will expect to see a different set of properties for a Process. A Process, on the other hand, has the following properties: ProcessName, Id (Process ID), MachineName (Computer running the process), etc.
Using the Get-Member Cmdlet to view the full list of an object’s properties and methods is very useful. This helps you understand what property to return in a report and how to manipulate the object using its methods.
For you to appreciate how important it is to have a way to find an object’s property, run the Get-Content command below (change the text file to another text file on your computer):
Get-Content D:\PS-Tutorial\websites.txt
The command simply lists the content of the text file. If this is all we know about this file, we cannot do more than use the content as input to another command.
The command below will reveal that there is more we can accomplish with this text file:
Get-Content D:\PS-Tutorial\websites.txt | Get-Member
As an example, if you scroll down towards the bottom of the results of this last command, you will see a Property called Length – see the second screenshot below. You will also see that there are multiple Methods we can use to manipulate the text file.
To see the magic of the Length Property, run the command below:
(Get-Content D:\PS-Tutorial\websites.txt).Length
This last command is the same as the command below. The only difference is that, in the command above, I want PowerShell to display the Length of the file – which is the size of the file in bytes.
The result is 46.
Get-Content D:\PS-Tutorial\websites.txt
Moving on, let’s example a Method for a text file called ToUpper. I have highlighted this Method in the screenshot below.
If you recollect, I said earlier that PowerShell Object’s Properties are used to display properties of the object. We have seen how this works in the Length Property example.
I also said that PowerShell Object’s Methods allow you to manipulate the object. I can use theToUpper Method to change the content of the text file to UPPER CASE – with the command below:
(Get-Content D:\PS-Tutorial\websites.txt).ToUpper()
Notice the difference between how I accessed the object’s Length Property and how I accessed its Method? The method has an additional item ‘()’ at the end of the command. The good news is that the output of the Get-Member command tells us how to access this method.
See where my arrow points in the screenshot below. This highlights the power of knowing how to access Objects Proparties and Methods using PowerShell.
Before I move to the next PowerShell tutorial, let me show you the result of the ToUpper Method.
(Get-Content D:\PS-Tutorial\websites.txt).ToUpper()
The screenshot below shows the result of the above command (right of the screenshot). On the left of the screenshot is the content of the text file.
The PowerShell command has changed the content of the text file to UPPER CASE!
How to Access Object Properties Using Variables
In the last tutorial, you saw how to access an object’s properties by enclosing the object (command) in a bracket followed by a period (.), then followed by the property you wish to access.
When you store the result of a command in a variable, accessing the properties of the object is less complicated. To show you how storing a command in a variable makes accessing its properties easier – let’s revisit the Get-ChildItem command from the last PowerShell tutorial –
Get-ChildItem D:\PS-Tutorial\websites.txt
Let’s store this command in a variable called $ObProp:
$ObProp = Get-ChildItem D:\PS-Tutorial\websites.txt
To access the properties of this object, type the variable name $ObProp, followed by a period (.) then press the TAB key.
The shell will display the properties and methods available for the object stored in the variable. To determine the size of the file, simply execute the command below:
$ObProp.Length
Additionally, the following commands will display the file’s name and the last time the file was accessed, respectively.
$ObProp.Name
$ObProp.LastAccessTime
Here are the results of these commands in PowerShell:
PowerShell Member Types and How to Use Them
In this tutorial, we will examine the different ‘MemberTypes’ and how you can use them.
Once again, let’s revisit our Get-ChildItem command, piped to the Get-Member command:
Get-ChildItem D:\PS-Tutorial\websites.txt | Get-Member
Here is the result of the command in PowerShell.
The ‘MemberType’ column has five distinct ‘Types’ – ‘CodeProperty’, ‘Method’, ‘NoteProperty’, ‘Property’ and ‘ScriptProperty’. We have already defined Method and Property. Below is a definition for the remaining three:
CodeProperty: A code property references a static property of a .NET Framework object.
NoteProperty: A note property defines a property that has a static value.
ScriptProperty: A script property defines a property whose value is the output of a script.
To help you understand some of these ‘MemberType’, lets take some examples.
There is a CodeProperty, ‘Mode’.
To display this CodeProperty, run the command below:
(Get-ChildItem D:\PS-Tutorial\websites.txt).Mode
The command result is ‘-a—-‘. What does this mean?
This value represents the file attributes setting of the text file. ‘-a—-‘ means that the ‘archive’ bit is set for this file. What is the ‘archive’ bit of a file?
To see this property, open the folder you saved the file. Then, right-click it and select Properties.
When the file Properties opens, click Advanced. Notice that File is ready for archiving is checked?
This is why the PowerShell command – (Get-ChildItem D:\PS-Tutorial\websites.txt).Mode – displayed the file Mode as ‘-a—-‘ – meaning that the archive bit is set.
The implication of this knowledge is that you can use PowerShell to change the archive bit of a file. This guide has the steps to perform this task – Use PowerShell to Toggle the Archive Bit on Files.
References and Further Reading for PowerShell Tutorial 7
This tutorial is based on my book with the same title. You can get it from Amazon.
I hope you found this PowerShell Tutorial helpful. If you found it helpful, vote Yes to the Was this post helpful? at the end of this tutorial.
You could also share your thoughts with the “Leave a Comment” form found at the end of this page.