The New-Object command in Windows PowerShell is a powerful tool that allows users to create .NET objects. It is one of the most important commands in the Windows PowerShell scripting language, as it allows users to create objects from a wide variety of sources. This article will explore the syntax, parameters, inputs, outputs, examples, and tips for using the New-Object command.

Syntax of the New-Object Command

The syntax of the New-Object command is as follows:

  • New-Object [-TypeName] <String> [-ArgumentList <Object[]>] [-Property <Hashtable>] [-PassThru] [-Force] [-EnableException] [<CommonParameters>]

The New-Object command has several parameters, which are described in the following section.

Parameters of the New-Object Command

The parameters of the New-Object command are as follows:

Parameter Description
-TypeName The type of object to create. This can be a .NET type, such as System.String or System.Int32, or a PowerShell type, such as PSCustomObject.
-ArgumentList An array of arguments to pass to the constructor of the type specified in the -TypeName parameter.
-Property A hashtable of properties to set on the object.
-PassThru If specified, the object created by the New-Object command will be passed through the pipeline.
-Force If specified, the New-Object command will create the object even if the type specified in the -TypeName parameter does not exist.
-EnableException If specified, the New-Object command will throw an exception if an error occurs.

Inputs of the New-Object Command

The inputs of the New-Object command are the parameters described above. The -TypeName parameter is required, and the other parameters are optional.

Outputs of the New-Object Command

The output of the New-Object command is an object of the type specified in the -TypeName parameter. If the -PassThru parameter is specified, the object will be passed through the pipeline.

Examples of the New-Object Command

The following are some examples of how to use the New-Object command:

  • To create a new string object, use the following command:
    • New-Object -TypeName System.String
  • To create a new PSCustomObject object with two properties, use the following command:
    • New-Object -TypeName PSCustomObject -Property @{Name=”John”; Age=30}
  • To create a new object and pass it through the pipeline, use the following command:
    • New-Object -TypeName System.String -PassThru

Tips for Using the New-Object Command

The following are some tips for using the New-Object command:

  • Make sure to specify the correct type in the -TypeName parameter. If the type does not exist, the New-Object command will fail.
  • If the type specified in the -TypeName parameter has a constructor that takes arguments, use the -ArgumentList parameter to pass the arguments.
  • If the type specified in the -TypeName parameter has properties that can be set, use the -Property parameter to set the properties.
  • If you want to pass the object created by the New-Object command through the pipeline, use the -PassThru parameter.
  • If you want the New-Object command to throw an exception if an error occurs, use the -EnableException parameter.

Leave a Reply