In scripts, I often use Methods to perform actions or retrieve information. In this small blog post, I will show you how to discover and use them 🙂
What are Methods?
“PowerShell uses objects to represent the items in data stores or the state of the computer. For example, FileInfo objects represent the files in file system drives and ProcessInfo objects represent the processes on the computer.
Objects have properties, which store data about the object, and methods that let you change the object.
A “method” is a set of instructions that specify an action you can perform on the object. For example, the FileInfo object includes the CopyTo method that copies the file that the FileInfo object represents.
To get the methods of any object, use the Get-Member cmdlet. Use its MemberType property with a value of “Method”. The following command gets the methods of process objects.
PowerShell
Get-Process | Get-Member -MemberType Method
Output
TypeName: System.Diagnostics.Process Name MemberType Definition ---- ---------- ---------- BeginErrorReadLine Method System.Void BeginErrorReadLine() BeginOutputReadLine Method System.Void BeginOutputReadLine() ... Kill Method System.Void Kill() Refresh Method System.Void Refresh() Start Method bool Start() ToString Method string ToString() WaitForExit Method bool WaitForExit(int milliseconds), ... WaitForInputIdle Method bool WaitForInputIdle(int millisecon...
To perform or “invoke” a method of an object, type a dot (.), the method name, and a set of parentheses “()”. If the method has arguments, place the argument values inside the parentheses. The parentheses are required for every method call, even when there are no arguments. If the method takes multiple arguments, they should be separated by commas.”
Checking for available methods
As mentioned in the chapter above, you can use Get-Member -MemberType Method to show the methods available for the Object that you want to do or see things of. For example, I stored all the Folder names in the $Folders variable and used Get-Member -MemberType Method on that to show the possible methods that I could use on the TypeName: System.IO.DirectoryInfo: (Returned by the Get-ChildItem Cmdlet)

This output will differ for each TypeName. In the example below, I did the same action on my running processes:

As you can see, the Methods (Actions) change to things that make sense for the TypeName System.Diagnostics.Process (Returned by the Get-Process Cmdlet)
Examples
Now that you know how to query the available Methods, I will show some examples of what you can do with them that I often use.
String
If you have a String Object, basically just some text values, you have these Methods available:

Examples for String Objects:
Replace
To replace a word in a String Object, you can use the Replace Method:

Split
If you want to split the String object into multiple Objects, you can use the Split Method:

The String Object has now been changed to an Array Object by using Space as its delimiter.
Substring
This will allow you to remove x number of characters from a string, for example:

Uppercase
To convert all text to Uppercase, you can use the ToUpper method:

TrimEnd
Folders
If you have a System.IO.DirectoryInfo Object (Returned as in my example above from using Get-ChildItem -Directory), you will have a few Methods available:

Delete
If you want to delete folders, be careful ;-), then you can use the Delete Method. I created three folders inside a Test folder, which I deleted using the Delete Method:

Note: As you can see, you can also enclose a command within ()s and execute the method like that, too.
GetFiles
This Method will return all the files from the Directory names stored in $Folders, which I created by running $Folders=Get-ChildItem -Directory from within my Downloads Folder:

Wrapping up
And this is how to discover and use Methods in a nutshell, you can do a lot more things with them, and each Object has different methods for you to discover. Have a lovely weekend!