Search code examples
powershellvariablesoperators

What does " :: " do and how do you use " :: " in powershell scripts?


Is it the equivalent of objectName.method or attribute in C#? An example or 2 would be helpful to learn exactly how to use this syntax of ::


Solution

  • From the about_Operators help topic:

    :: Static member operator
        Calls the static properties operator and methods of a .NET
        Framework class. To find the static properties and methods of an
        object, use the Static parameter of the Get-Member cmdlet.
       
        [datetime]::now
    

    That's basically it.

    The static member operator takes a type literal on the left hand side, and allows access to that type's static members (methods and properties alike):

    # The PowerShell class can only be instantiated through a static method called Create()
    $psInstance = [powershell]::Create()
    

    You can also use it on a variable containing a type:

    $dt = [datetime]
    $UtcTimestamp = $dt::UtcNow