Behind the PowerShell Pipeline

Share this post

Climbing the PowerShell Class Family Tree

jeffhicks.substack.com

Climbing the PowerShell Class Family Tree

Jeff Hicks
Mar 23, 2023
∙ Paid
Share
Share this post

Climbing the PowerShell Class Family Tree

jeffhicks.substack.com

When I started this series, I pointed out that a PowerShell class, while easy to define, lacks all the features of a traditional class defined in a language like C#. Fortunately, there is a class feature you can take advantage of, although I would venture to say it is for special use cases, which is inheritance.

PowerShell uses inheritance all the time. Often a class is defined from a parent class. And as in biology, the child class inherits values from the parent. In this case, these are items such as properties and methods. The child class is further defined by properties and methods unique to it.

You can see this hierarchy of classes by looking at an object’s type name.

PS C:\> $w = get-item c:\work
PS C:\> $w.psobject.typenames
System.IO.DirectoryInfo
System.IO.FileSystemInfo
System.MarshalByRefObject
System.Object

Inheritance runs from top to bottom. The object is a DirectoryInfo type that inherits settings from a FileSystemInfo object. We can use the Get-TypeMember command from the PSScriptTools module to see what is different.

PS C:\> $parent = Get-TypeMember system.io.filesysteminfo
PS C:\> $child = Get-TypeMember system.io.DirectoryInfo
PS C:\> Compare-Object $parent $child -Property name

name                     SideIndicator
----                     -------------
Create                   =>
CreateSubdirectory       =>
EnumerateDirectories     =>
EnumerateFiles           =>
EnumerateFileSystemInfos =>
GetDirectories           =>
GetFiles                 =>
GetFileSystemInfos       =>
MoveTo                   =>
Parent                   =>
Root                     =>

The member type is relatively clear, but let’s be specific.

PS C:\> Compare-Object $parent $child -Property name | Foreach-Object { 
 $n = $_.name
 $child | Where-Object {$_.name -eq $n}
}

   Type: System.IO.DirectoryInfo

Name                     MemberType ResultType       IsStatic
----                     ---------- ----------       --------
Create                   Method     Void                False
CreateSubdirectory       Method     DirectoryInfo       False
EnumerateDirectories     Method     IEnumerable`1       False
EnumerateFiles           Method     IEnumerable`1       False
EnumerateFileSystemInfos Method     IEnumerable`1       False
GetDirectories           Method     DirectoryInfo[]     False
GetFiles                 Method     FileInfo[]          False
GetFileSystemInfos       Method     FileSystemInfo[]    False
MoveTo                   Method     Void                False
Parent                   Property   DirectoryInfo
Root                     Property   DirectoryInfo

These members are added to the DirectoryInfo class. These members are inherited.

PS C:\> Compare-Object $parent $child -Property name -ExcludeDifferent

name                      SideIndicator
----                      -------------
CreateAsSymbolicLink      ==
Delete                    ==
Equals                    ==
GetHashCode               ==
GetLifetimeService        ==
GetObjectData             ==
GetType                   ==
InitializeLifetimeService ==
Refresh                   ==
ResolveLinkTarget         ==
ToString                  ==
Attributes                ==
CreationTime              ==
CreationTimeUtc           ==
Exists                    ==
Extension                 ==
FullName                  ==
LastAccessTime            ==
LastAccessTimeUtc         ==
LastWriteTime             ==
LastWriteTimeUtc          ==
LinkTarget                ==
Name                      ==
UnixFileMode              ==

Our task now is to achieve the same type of result using a PowerShell class.

Keep reading with a 7-day free trial

Subscribe to Behind the PowerShell Pipeline to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
Previous
Next
© 2023 JDH Information Technology Solutions, Inc.
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing