How To Use .NET Properties and Methods in PowerShell

Learn about .NET properties and methods and how they can help you create more dynamic scripts.

Brien Posey

June 12, 2024

3 Min Read
abstract image of cogs and diagonal lines
Alamy

In this series, we have thoroughly discussed .NET namespaces and classes. Now, let’s shift our focus to properties and methods.

Advanced PowerShell Techniques with the .NET Framework

Before diving in, here’s the sample PowerShell script we have been using:

ray-so-export.png

Exploring .NET Properties

The sample script has several properties, such as $Form.Width and $Form.Height. Both Width and Height are properties of the Form class.

To explore properties and methods, visit the .NET API browser. Click on a namespace to see its classes, then select a class to view its associated elements, such as properties, methods, fields, and events.

Properties are essentially attributes that can be applied to an object. Our sample script sets a button’s text and background color using properties.

Properties That Reference Methods

Occasionally, you may find that properties reference methods. Consider this line of code from the script:

$Form.controls.add($Button1)

This line associates the button object with the form object. Unlike other properties discussed, it doesn’t consist solely of a class name and a property name separated by a period. Instead, there is a reference to the previously created form object, followed by a property name (controls), a period, and a method name (add). This construction is necessary because the Form class lacks an Add method. However, it does have a Controls property. This property contains the ControlCollection (Microsoft explains this here), from which the Add method is accessed.

Believe it or not, figuring out the right classes, properties, and methods is the hardest part. The actual syntax is fairly intuitive.

When you click on a property or a method in the .NET API browser, it opens a page providing syntax and examples, typically written in C#. Even though the examples are in C#, they are useful for understanding the values needed for properties or methods. For instance, in Figure 1, Form.Width requires an integer value representing the form’s width in pixels.

screenshot of the .NET API browser interface

Figure 1. Help is available for all the properties and methods.

Calling a Method Directly

We’ve covered basic techniques for working with classes, properties, and methods. Let’s introduce one more concept. In previous examples, I used the New-Object cmdlet to create an object linked to a namespace and a class, such as:

$Form = New-Object System.Windows.Forms.Form

Although this is the normal way of doing things, some classes lack a required constructor, so you can’t use them with the New-Object cmdlet. For instance, to check if a file named C:\test.txt exists on my hard drive, I might try:

$File = New-Object System.IO.File
$File.Exists("C:\Temp\Test.txt”)

Here, System.IO is the namespace, File is a class, and Exists is a method within the File class. The Exists method requires a filename (and optional path) in string format and returns True if the file exists or False if it doesn’t.

However, the File class lacks the required constructor, resulting in an error (see Figure 2).

PowerShell screenshot demonstrates error when attempting to create an object using File class

Figure 2. You cannot create an object using the File class.

To work around this, call the method directly. We could use this command in this case:

[System.IO.File]::Exists(“C:\Temp\Test.txt”)

Here, the namespace and the class name are enclosed in brackets. The two colons indicate to PowerShell that a .NET method is being called. Append the method name just after the two colons and supply any required parameters (in this case, a path and filename). See what the command looks like in Figure 3.

PowerShell screenshot shows [System.IO.File]::Exists(“C:\Temp\Test.txt”) command

Figure 3. You can access some .NET methods directly.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like