Search code examples
windowspowershellwindows-server-2016

why Parentheses used for in this powershell command


I'm new with Powershell can someone explaine to me why Parentheses and point are used for :

(get-cluster).PreferredSite="SiteA"

why not just :

get-cluster | set-preferredSite -Name SiteA

Solution

  • Set-preferredSite looks like the (hypothetical) name of a cmdlet, and, as TessellatingHeckler points out, it would be practically impossible to build a cmdlet for each and every property name (leaving aside the fact that there can be dynamically created properties whose names you cannot predict).

    Therefore, you have no choice but to use PowerShell's syntax for assigning a value to an object's property, which generally requires the use of an expression, which is not CLI-like (argument mode) but programming-language-like (expression mode).

    PowerShell v3+ does offer a less "noisy" syntax with its so-called operation statement; e.g.:

    Get-Cluster | % PreferredSite
    

    which is the equivalent of the more verbose:

    Get-Cluster | % { $_.PreferredSite }
    

    However, operation statements do not support assignments, so the following does NOT work:

    Get-Cluster | % PreferredSite = 'SiteA' # !! Does NOT work.
    

    My guess as to why that is not supported is that it's rare to want to use the pipeline to set the property of each item in a potentially large input set to the same value.

    For a smallish collection, (Get-Cluster).PreferredSite="SiteA" will do just fine, even though the output from Get-Cluster is collected in memory as a whole first (if Get-Cluster outputs more than 1 object, .PreferredSite is accessed on each item in the collection in PSv3+, a feature called member-access enumeration).

    For large collections, use % (ForEach-Object) with a custom script block:

    Get-Cluster | % { $_.PreferredSite = 'SiteA' }
    

    Note how:

    • the statement inside { ... } is an assignment expression.
    • $_ is the automatic variable that refers to the input object at hand in each iteration.