nullByteMe
nullByteMe

Reputation: 6391

Difference when using [Some.Object] to create an object in powershell

What is the difference between creating an object this way:

$form.Size = New-Object [System.Drawing.Size] 250, 250

and this way:

$form.Size = New-Object System.Drawing.Size 250, 250

The first one fails, but the second one does not. Are the brackets only to be used when calling static values from an object?

Upvotes: 4

Views: 885

Answers (2)

mjolinor
mjolinor

Reputation: 68273

The help on New-Object says:

Synopsis
    Creates an instance of a Microsoft .NET Framework or COM object.

Syntax
    New-Object [-TypeName] <String> [[-ArgumentList] <Object[]>] [-Property <IDictionary>] [<CommonParameters>]

Note that the argument for Typename is string. When you include the square bracktets, it's looking for a typename that literally contains square brackets.

Upvotes: 2

user189198
user189198

Reputation:

When you put square brackets around the .NET type name, you get a reference to the .NET type itself. New-Object is expecting a string representation of the .NET type that you're instantiating, which gets mapped to the -TypeName parameter.

You use the square brackets to reference a .NET type, for example, if you are calling a static method on a class.

[System.Net.Dns]::GetHostEntry('www.google.com');

Upvotes: 8

Related Questions