Working with PowerShell Static and Instance Methods
PowerShell is built on .NET, and many powerful .NET classes. Everything you do in PowerShell accepts and returns .NET objects. Every object is built from a class in the Microsoft .NET framework, which defines the structure and behavior, more or less like a blueprint.
An object will have an object type, properties and methods. Methods allow you to interact with objects in a dynamic way. In PowerShell you can also work with static classes, you can create an object from a static class but you can access their methods and properties.
In this post, we’ll explore two types of methods: Instance and static.
What Are Instance Methods?
Even though in the PowerShell documentation it states just the word method when working with objects, I wanted to differentiate it from the static methods and therefore call them instance methods.
You can see the properties and methods of an object by piping to to Get-Member, for example:
Get-Service | Get-MemberYou can also specify the member type to see just the methods:
Get-Service | Get-Member -MemberType MethodTo use a method on an object you need to use a (.), the method name and a set of parenthesis. For example:
"hello world".ToUpper()Here, "hello world" is a .NET String object. This is an example where the method is tied to an instance of an object. This method is tied to a specific string instance, which is why we use dot notation.
What Are Static Methods?
You can also use the static method of a .NET class, for example:
# Static method example
[Math]::Pow(2, 3) # Output: 8The Math class is an example of a class that you simply use them without creating an instance of it. To use a static method (or property) you need to tell PowerShell by using :: after the class name.
Since a class is not an object you will need to use the following to see the static methods:
[System.Math] | Get-Member -Static -MemberType MethodsSummary of each method
Instance Methods
Require an object to be created first.
Can access and modify instance properties.
Static Methods
Belong to the class itself, not any one object.
Called directly on the class.
Cannot access instance-specific data.
Method Arguments
You will notice that some methods will accept arguments. Arguments are like Cmdlet parameters. You include arguments between the parenthesis, for example if you wanted to replace a string with another string.
"hello world".Replace("world", "PowerShell")You will be able to see the what a method accepts when you use Get-member.
"string" | Get-Member -Name replace
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Replace Method string Replace(string oldValue, string newValue, bool ignoreCase, cultureinfo culture) ...If multiple arguments are accepted like with the Replace method, you will need to separate them with commas.
Get-Date | Get-Member -MemberType Method
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Add Method datetime Add(timespan value)
AddDays Method datetime AddDays(double value)
AddHours Method datetime AddHours(double value)For the AddDays method you will notice it will accept a value of the type Double.
Real-World Examples
Below are some examples of methods I use regularly.
String Methods
Object:
"hello".ToUpper()→"HELLO""hello world".Replace("world", "PowerShell")" hello world ".Trim()→"hello world".Substring()
Static:
[String]::IsNullOrWhiteSpace(" ")→True[String]::IsNullOrEmpty($text)→ boolean check.
Date/Time Methods
Object:
(Get-Date).AddDays(7)→ date one week from now(Get-Date).AddDays(-7)→ date one week ago(Get-Date).ToShortDateString()→ dd/MM/yyyy
Static:
[DateTime]::Now→ current date/time[DateTime]::UtcNow→ current UTC time
Math Methods (Static)
[Math]::Round(3.14159, 2)→3.14[Math]::Max(5, 10)→10
Environment (Static)
[System.Environment]::OSVersionHashtables
One useful method which I use when working with hashtables is GetEnumerator(). This method gives you each key/value pair one after another. Because a hashtable gets treated as one object when you pipe it you can’t simply pipe it to ForEach-Object
# Create a hashtable
$person = @{
Name = "Alice"
Age = 30
City = "Berlin"
}
# Get the enumerator and Iterate through key/value pairs
$person.GetEnumerator() | ForEach-Object{
"$($_.key) is $($_.value)"
}You could even use the foreach method:
$person.GetEnumerator().foreach({"$($_.key) is $($_.value)"})Final Thought
Methods are very handy to use in scripts and in the command line. Although you have things like the -split and -replace operators which work with strings, there are many more methods you can use when working with strings and to my knowledge they might be the only way to do certain actions. In this post I wanted to differentiate the two types of methods and some use cases.
