Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ::

like image 480
King of NES Avatar asked Jul 20 '16 14:07

King of NES


1 Answers

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
like image 172
Mathias R. Jessen Avatar answered Sep 28 '22 15:09

Mathias R. Jessen