About Object Creation


Explains how to create objects in PowerShell.


You can create objects in PowerShell and use the objects that you create in commands and scripts.

There are many ways to create objects, this list is not definitive:

  • New-Object: Creates an instance of a .NET Framework object or COM object.
  • Import-Csv/ ConvertFrom-CSV: Creates custom objects (PSCustomObject) from the items defined as comma separated values.
  • ConvertFrom-Json: Creates custom objects defined in JavaScript Object Notation (JSON).
  • ConvertFrom-String: Built on top of FlashExtract, ConvertFrom-String creates custom objects from structured string data. This topic will demonstrate and discuss each of these methods.
  • ConvertFrom-StringData: Creates custom objects defined as key value pairs.
  • Add-Type: Allows you to define a class in your PowerShell session that you can instantiate with New-Object.
  • New-Module: The AsCustomObject parameter creates a custom object you define using script block.
  • Add-Member: Adds properties to existing objects. You can use Add-Member to create a custom object out of a simple type, like [System.Int32].
  • Select-Object: Selects properties on an object. You can use Select-Object to create custom and calculated properties on an already instantiated object.

The following additional methods are covered in this article:

  • Static new operator: Beginning in PowerShell 5.0, you can create objects by calling a type's constructor using a static new operator.
  • System.Activator class: Creates objects given the assembly name and type name.
  • Hash tables: Beginning in PowerShell 3.0, you can create objects from hash tables of property names and property values.

Determining constructors for a type

You can determine the available constructors for a given type using the following sample script.

function Get-Constructors ([type]$type)
    foreach ($constr in $type.GetConstructors())
        $params = ''
        foreach ($parameter in $constr.GetParameters())
            if ($params -eq '') {
                $params =  "{0} {1}" -f $parameter.parametertype.fullname,
            } else {
              $params +=  ", {0} {1}" -f $parameter.parametertype.fullname,
        Write-Host $($constr.DeclaringType.Name) "($params)"
Get-Constructors "System.String"
String (System.Char[] value)
String (System.Char[] value, System.Int32 startIndex, System.Int32 length)
String (System.Char* value)
String (System.Char* value, System.Int32 startIndex, System.Int32 length)
String (System.SByte* value)
String (System.SByte* value, System.Int32 startIndex, System.Int32 length)
String (System.SByte* value, System.Int32 startIndex, System.Int32 length, System.Text.Encoding enc)
String (System.Char c, System.Int32 count)
String (System.ReadOnlySpan`1[[System.Char, System.Private.CoreLib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] value)

Static new() method

Beginning in PowerShell 5.0, all .NET types have an added new operator which allows you to construct instances more easily. You can also see all the available constructors for a given type.

To see the constructors for a type, specify the new static method after the type name and press <ENTER>.

uri new(string uriString)
uri new(string uriString, bool dontEscape)
uri new(uri baseUri, string relativeUri, bool dontEscape)
uri new(string uriString, System.UriKind uriKind)
uri new(uri baseUri, string relativeUri)
uri new(uri baseUri, uri relativeUri)

Now, you can create a System.Uri by specifying the appropriate constructor.

AbsolutePath   : /
AbsoluteUri    : https://www.bing.com/
LocalPath      : /
Authority      : www.bing.com

You can use the following sample to determine what .NET types are currently loaded for you to instantiate.

[AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {
                $_.GetExportedTypes() | ForEach-Object { $_.FullName }
} | Out-Host -Paging

System.Activator class

The System.Activator class allows you to create a type by specifying the assembly and type name.

You can view the static methods of the System.Activator class using Get-Member with the Static parameter.

[System.Activator] | Get-Member -Static

The following example creates a System.Diagnostics.Stopwatch using the CreateInstance static method which takes a type and array of arguments. The System.Diagnostics.Stopwatch constructor takes no arguments, so we pass an empty array.

[System.Activator]::CreateInstance([System.Diagnostics.Stopwatch], @())
IsRunning Elapsed  ElapsedMilliseconds ElapsedTicks
--------- -------  ------------------- ------------
    False 00:00:00                   0            0


Beginning in PowerShell 3.0, you can create an object from a hash table of properties and property values.

The syntax is as follows:


This method works only for classes that have a null constructor, that is, a constructor that has no parameters. The object properties must be public and settable.


Custom objects are very useful and are easy to create using the hash table method. The PSCustomObject class is designed specifically for this purpose.

Custom objects are an excellent way to return customized output from a function or script. This is more useful than returning formatted output that cannot be reformatted or piped to other commands.

The commands in the Test-Object function set some variable values and then use those values to create a custom object. You can see this object in use in the example section of the Update-Help cmdlet help topic.

function Test-Object {
  $ModuleName = "PSScheduledJob"
  $HelpCulture = "en-us"
  $HelpVersion = ""
  $ModuleName = "PSWorkflow"
  $HelpCulture = "en-us"
  $HelpVersion = ""

The output of this function is a collection of custom objects formatted as a table by default.

ModuleName        UICulture      Version
---------         ---------      -------
PSScheduledJob    en-us
PSWorkflow        en-us

Users can manage the properties of the custom objects just as they do with standard objects.



You can also use hash tables to create objects for non-custom classes. When you create an object for a non-custom class, the full namespace name is required unless class is in the System namespace. Use only the properties of the class.

For example, the following command creates a session option object.


The requirements of the hash table feature, especially the null constructor requirement, eliminate many existing classes. However, most PowerShell option classes are designed to work with this feature, as well as other very useful classes, such as the ScheduledJobTrigger class.

Id   Frequency   Time                   DaysOfWeek  Enabled
--   ---------   ----                   ----------  -------
0    Daily       6/6/2012 3:00:00 PM                True

You can also use the hash table feature when setting parameter values. For example, the value of the SessionOption parameter of the New-PSSession cmdlet and the value of the JobTrigger parameter of Register-ScheduledJob can be a hash table.

New-PSSession -ComputerName Server01 -SessionOption @{
Register-ScheduledJob Name Test -FilePath .\Get-Inventory.ps1 -Trigger @{

Generic Objects

You can also create generic objects in PowerShell. Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use.

The following example creates a Dictionary object.

$dict = New-Object 'System.Collections.Generic.Dictionary[String,Int]'
$dict.Add("One", 1)
Key Value
--- -----
One     1

For more information on Generics, see Generics in .NET.