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

后端 未结 4 903
梦谈多话
梦谈多话 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:25

    The "." dot sourcing operator will send AND receive variables from other scripts you have called. The "&" call operator will ONLY send variables.

    For instance, considering the following:

    Script 1 (call-operator.ps1):

    clear
    
    $funny = "laughing"
    
    $scriptpath = split-path -parent $MyInvocation.MyCommand.Definition
    $filename = "laughing.ps1"
    
    "Example 1:" # Call another script. Variables are passed only forward.
    
    & $scriptpath\$filename
    
    "Example 2:" # Call another script. Variables are passed backwards and forwards.
    
    . $scriptpath\$filename
    $variableDefinedInOtherScript
    

    Script 2 (laughing.ps1):

    # This is to test the passing of variables from call-operator.ps1
    
    "I am $funny so hard. Passing variables is so hilarious."
    
    $variableDefinedInOtherScript = "Hello World!"
    

    Create both scripts and ONLY run the first one. You'll see that the "." dot sourcing operator sends and receives variables.

    Both have their uses, so be creative. For instance, the "&" call operator would be useful if you wanted to modify the value(s) of variables in another script while preserving the original value(s) in the your current script. Kinda a safeguard. ;)

    0 讨论(0)
  • 2020-12-28 14:35

    .Period or .full stop for an objects properties; like

    $CompSys.TotalPhysicalMemory
    

    See here: http://www.computerperformance.co.uk/powershell/powershell_syntax.htm#Operators_

    0 讨论(0)
  • 2020-12-28 14:38

    The dot is a call operator:

    $a = "Get-ChildItem" 
    . $a # (executes Get-ChildItem in the current scope)
    

    In your case, however, I don't see what it does.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题