Why isn’t “New-Object” aliased to “New”

Why isn’t “New-Object” aliased to “New”

  • Comments 7

Alistair Young made a Note to Self :

In a standard PowerShell installation, "new" is not an alias for "new-object". You added that. You should therefore know better that to make this kind of dumbass mistake.

We talked about making "New" an alias to "New-Object" but decided against it in the end because it leaves open the door for us to provide a C#-like NEW keyword sometime in the future. We want PowerShell to provide a nice glide path to C# so in the future, we'd like the following to be equivalent:

$x = New-Object System.DateTime 2007,12,25

$x = new System.DateTime(2007,12,25)

So if we decide to implement this in the future and you've aliased New to New-Object, your scripts will break.

 

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post
  • PingBack from http://www.articlefeed.net/why-isn%e2%80%99t-%e2%80%9cnew-object%e2%80%9d-aliased-to-%e2%80%9cnew%e2%80%9d/

  • What would be the difference between making that an alias and making that a keyword, syntactically speaking?

    Do you really want us to memorize which keywords use the C style method call syntax and which keywords use the shell style?

    I've been using PowerShell on-and-off for probably about a year now and I still don't feel comfortable with it because of all these weird subtleties.

  • Thanks for the information, and I guess I'll be taking that out of my profile now!

  • As some one who uses both Powershell and C# I would really like to see the NEW keyword.  It would make the life of those using both so much simpler.  Please.

  • > What would be the difference between making that an alias and making that a keyword, syntactically speaking?

    Aliases are for pure text substitutions for Cmdlets, Functions, scripts or native executables.  Aliases have no effect on the parser. Keywords are understood by the parser and define the syntax of the subsequent characters.

    > Do you really want us to memorize which keywords use the C style method call syntax and which keywords use the shell style?

    If and when we add the NEW keyword - it will be purely an optional syntax for those people that want their PowerShell code to be more like C#.  You should just use New-Object and that will always work.

    Jeffrey Snover [MSFT]

    Windows PowerShell/MMC Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • That doesn't really change the fact that we need to learn to distinguish the two in order to become comfortable with the language though. Just because we don't need to use it doesn't mean we won't ever come across it.

    At first I was looking forward to the "new" keyword, but now personally I think I'll vote against it :)

    Thanks for being so responsive on this blog though, even if I don't always get things my way at least I know you're making decisions with our concerns in mind.

  • I am not familiar with C#, what difference is there between NEW and new-object other than using parenthesis for the parameters? If so, the syntax provided in your example with NEW would also work with new-object:

    $x = new-object system.datetime(2007,12,25)

    Therefore aliasing new-object to NEW wouldn't break anything when you introduce the NEW keyword if that is the only expected difference.

    Thanks,

    Jacques

Page 1 of 1 (7 items)