PowerShell


PowerShell Classes 5.0

1.0
2.0
3.0
4.0
5.0
5.1 Preview

This draft deletes the entire topic.

expand all collapse all

Examples

  • 5
    5.0

    In PowerShell 5.0+ you can list available constructors by calling the static new-method without parentheses.

    PS> [DateTime]::new
    
    OverloadDefinitions
    -------------------
    datetime new(long ticks)
    datetime new(long ticks, System.DateTimeKind kind)
    datetime new(int year, int month, int day)
    datetime new(int year, int month, int day, System.Globalization.Calendar calendar)
    datetime new(int year, int month, int day, int hour, int minute, int second)
    datetime new(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind kind)
    datetime new(int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar)
    datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond)
    datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond, System.DateTimeKind kind)
    datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
    System.Globalization.Calendar calendar)
    datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
    System.Globalization.Calendar calendar, System.DateTimeKind kind)
    

    This is the same technique that you can use to list overload definitions for any method

    > 'abc'.CompareTo
    
    OverloadDefinitions
    -------------------
    int CompareTo(System.Object value)
    int CompareTo(string strB)
    int IComparable.CompareTo(System.Object obj)
    int IComparable[string].CompareTo(string other)
    

    For earlier versions you can create your own function to list available constructors:

    function Get-Constructor {
        [CmdletBinding()]
        param(
            [Parameter(ValueFromPipeline=$true)]
            [type]$type
        )
    
        Process {
            $type.GetConstructors() | 
            Format-Table -Wrap @{
                n="$($type.Name) Constructors"
                e={ ($_.GetParameters() | % { $_.ToString() }) -Join ", " }
            }
        }
    }
    

    Usage:

    Get-Constructor System.DateTime    
    #Or [datetime] | Get-Constructor
    
    DateTime Constructors
    ---------------------
    Int64 ticks
    Int64 ticks, System.DateTimeKind kind
    Int32 year, Int32 month, Int32 day
    Int32 year, Int32 month, Int32 day, System.Globalization.Calendar calendar
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.DateTimeKind kind
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.Globalization.Calendar calendar
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.DateTimeKind kind
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.Globalization.Cal
    endar calendar
    Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.Globalization.Cal
    endar calendar, System.DateTimeKind kind
    
  • 1
    class Person {
        [string] $Name
        [int] $Age
    
        Person([string] $Name) {
            $this.Name = $Name
        }
    
        Person([string] $Name, [int]$Age) {
            $this.Name = $Name
            $this.Age = $Age
        }
    }
    
  • 1
    class Person {
        [string] $FirstName
        [string] $LastName
        [string] Greeting() {
            return "Greetings, {0} {1}!" -f $this.FirstName, $this.LastName
        }
    }
    
    $x = [Person]::new()
    $x.FirstName = "Jane"
    $x.LastName = "Doe"
    $greeting = $x.Greeting() # "Greetings, Jane Doe!"
    

I am downvoting this example because it is...

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about PowerShell Classes? Ask Question

Listing available constructors for a class

5
5.0

In PowerShell 5.0+ you can list available constructors by calling the static new-method without parentheses.

PS> [DateTime]::new

OverloadDefinitions
-------------------
datetime new(long ticks)
datetime new(long ticks, System.DateTimeKind kind)
datetime new(int year, int month, int day)
datetime new(int year, int month, int day, System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second)
datetime new(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind kind)
datetime new(int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond, System.DateTimeKind kind)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar, System.DateTimeKind kind)

This is the same technique that you can use to list overload definitions for any method

> 'abc'.CompareTo

OverloadDefinitions
-------------------
int CompareTo(System.Object value)
int CompareTo(string strB)
int IComparable.CompareTo(System.Object obj)
int IComparable[string].CompareTo(string other)

For earlier versions you can create your own function to list available constructors:

function Get-Constructor {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [type]$type
    )

    Process {
        $type.GetConstructors() | 
        Format-Table -Wrap @{
            n="$($type.Name) Constructors"
            e={ ($_.GetParameters() | % { $_.ToString() }) -Join ", " }
        }
    }
}

Usage:

Get-Constructor System.DateTime    
#Or [datetime] | Get-Constructor

DateTime Constructors
---------------------
Int64 ticks
Int64 ticks, System.DateTimeKind kind
Int32 year, Int32 month, Int32 day
Int32 year, Int32 month, Int32 day, System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.DateTimeKind kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.DateTimeKind kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.Globalization.Cal
endar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, System.Globalization.Cal
endar calendar, System.DateTimeKind kind

Constructor overloading

1
class Person {
    [string] $Name
    [int] $Age

    Person([string] $Name) {
        $this.Name = $Name
    }

    Person([string] $Name, [int]$Age) {
        $this.Name = $Name
        $this.Age = $Age
    }
}

Methods and properties

1
class Person {
    [string] $FirstName
    [string] $LastName
    [string] Greeting() {
        return "Greetings, {0} {1}!" -f $this.FirstName, $this.LastName
    }
}

$x = [Person]::new()
$x.FirstName = "Jane"
$x.LastName = "Doe"
$greeting = $x.Greeting() # "Greetings, Jane Doe!"

Basic Class Template

0
# Define a class
class TypeName
{
   # Property with validate set
   [ValidateSet("val1", "Val2")]
   [string] $P1

   # Static property
   static [hashtable] $P2

   # Hidden property does not show as result of Get-Member
   hidden [int] $P3

   # Constructor
   TypeName ([string] $s)
   {
       $this.P1 = $s       
   }

   # Static method
   static [void] MemberMethod1([hashtable] $h)
   {
       [TypeName]::P2 = $h
   }

   # Instance method
   [int] MemberMethod2([int] $i)
   {
       $this.P3 = $i
       return $this.P3
   }
}

Get All Members of an Instance

0
PS > Get-Member -InputObject $anObjectInstance

This will return all members of the type instance. Here is a part of a sample output for String instance

   TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB), i...
Contains         Method                bool Contains(string value)
CopyTo           Method                void CopyTo(int sourceIndex, char[] destination, int destinationI...
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.S...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool E...
GetEnumerator    Method                System.CharEnumerator GetEnumerator(), System.Collections.Generic...
GetHashCode      Method                int GetHashCode()
GetType          Method                type GetType()
...

Topic Outline