What does the period '.' operator do in powershell?

后端 未结 4 968
梦谈多话
梦谈多话 2020-12-28 13:46

This is a weird one. Normally when I execute an external command from powershell I use the & operator like this:

& somecommand.exe -p so         


        
4条回答
  •  温柔的废话
    2020-12-28 14:41

    The Short: It is a Special Operator used to achieve what regular operators cannot achieve. This particular operator . actually has two distinctively different Special Operator use cases.

    The Long:

    As with any other language, scripting or otherwise, PowerShell script also supports many different types of Operators to help manipulate values. These regular operators include:

    • Arithmetic
    • Assignment
    • Comparison
    • Logical
    • Redirection
    • List item
    • Split and Join
    • Type
    • Unary

    However, PowerShell also supports whats known as Special Operators which are used to perform tasks that cannot be performed by the other types of operators.

    These Special Operators Include:

    • @() Array subexpression operator
    • & Call operator
    • [ ] Cast operator
    • , Comma operator
    • . Dot sourcing operator
    • -f Format operator
    • [ ] Index operator
    • | Pipeline operator
    • . Property dereference operator
    • .. Range operator
    • :: Static member operator
    • $( ) Subexpression operator

    . Dot sourcing operator: is used in this context to allow a script to run in the current scope essentially allowing any functions, aliases, and variables which has been created by the script to be added to the current script.

    Example:

    . c:\scripts.sample.ps1 
    

    NoteThat this application of the . Special Operator is followed by a space to distinguish it from the (.) symbol that represents the current directory

    Example:

    . .\sample.ps1
    

    . Property dereference operator: Allows access to the properties and methods of of an object which follows the . by indicating that the expression on the left side of the . character is an object and the expression on the right side of the is an object member (a property or method).

    Example:

    $myProcess.peakWorkingSet  
    (get-process PowerShell).kill()
    

    Disclaimer & Sources:

    I had the same question while looking at a PowerShell script that I was trying to expand on its feature sets and landed here when doing my research for the answer. However I managed to find my answer using this magnificent write up on the Microsoft Development Network supplemented with this further expansion of the same ideas from IT Pro.

    Cheers.

提交回复
热议问题