Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell 3.0 is supposed to have cleaner syntax, what does it look like?

I've seen mentions of improved PowerShell 3.0 syntax but not an example yet, how will it look like?

like image 288
Borek Bernard Avatar asked Oct 17 '11 22:10

Borek Bernard


People also ask

How do you clean PowerShell?

How to clear the PowerShell screen. As discussed earlier, the PowerShell clear screen action is supported by a function Clear-Host and its two aliases cls and clear.

How do you read PowerShell syntax?

Windows PowerShell uses a verb-noun pair as a naming system. The name of each cmdlet includes a standard verb hyphenated with a specific noun. Verbs are used for specific actions in Windows PowerShell, and nouns are used to describe objects by users and system administrators.

How do you clear all PowerShell commands?

Clear-History deletes the command history from a PowerShell session. Each PowerShell session has its own command history. To display the command history, use the Get-History cmdlet. By default, Clear-History deletes the entire command history from a PowerShell session.


1 Answers

A number of the common *-Object cmdlets utilize multiple parameter sets to accomplish the simplified syntax. Take a look at this in V3:

C:\PS> Get-Command Where-Object -Syntax

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] [-EQ] [<CommonParameters>]

Where-Object [-FilterScript] <scriptblock> [-InputObject <psobject>] [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CGT [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -LT [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CEQ [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -GT [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLT [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -GE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CGE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -LE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLE [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Like [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLike [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotLike [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotLike [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Match [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CMatch [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotMatch [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotMatch [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Contains [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CContains [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotContains [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotContains [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -In [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CIn [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotIn [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotIn [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Is [<CommonParameters>]

Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -IsNot [<CommonParameters>]

NOTE: Check out the new operators -NotIn and -In e.g.:

C:\PS> 1 -In 1..5
C:\PS> 10 -NotIn 1..5

So the simplified syntax is nice for the "common" case but watch out as you can fall off into the sharp rocks and lava pretty easily e.g.:

C:\PS> Get-ChildItem | Where LastWriteTime.Year -eq 2010

This returns nothing and even worse, there is no error so you think the result set is "correctly" empty when in fact this syntax just doesn't work as you might expect. That is, you can't access a property of a property. In the above, PowerShell looks for a property called LastWriteTime.Year which doesn't exist.

Also note that as part of the simplified syntax you can now use $PSItem in place of $_ in case you or those you write scripts for have some sort of allergic reaction to $_. :-)

And while this isn't necessarily tied to the simplified syntax I find that it simplifies my life and I love it:

C:\PS> Get-ChildItem -Directory
C:\PS> Get-ChildItem -File
C:\PS> dir -ad
C:\PS> Get-ChildItem -Attributes System+Hidden+Directory+!Archive
like image 115
Keith Hill Avatar answered Oct 13 '22 11:10

Keith Hill