Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between dot (.) and ampersand (&) in PowerShell?

Tags:

powershell

In PowerShell, what is the difference between using dot (.) and ampersand (&) when invoking a cmdlet, function, script file, or operable program?

For example:

. foo.sh1
& foo.sh1

There is a very similar question which has been incorrectly closed as a duplicate: Differences between ampersand (&) and dot (.) while invoking a PowerShell scriptblock. The questions are different and have completely different keywords and search rankings. The answer on What is the `.` shorthand for in a PowerShell pipeline? only answers half the question.

like image 816
steinybot Avatar asked Feb 13 '19 03:02

steinybot


People also ask

What does the ampersand mean in R&D?

In common shorthand expressions like “R&D,” “rock & roll,” or “country & western.”. The ampersand can be used to indicate that the “and” in a listed item is a part of the item’s name and not a separator (e.g. “Rock, pop, rhythm & blues and hip hop”).

What is the difference between the dot (.) operator and-> in C++?

What is the difference between the dot (.) operator and -> in C++? The dot and arrow operator are both used in C++ to access the members of a class. They are just used in different scenarios. In C++, types declared as class, struct, or union are considered "of class type".

What does the ampersand mean in film credits?

In film credits for stories, screenplays, etc., & indicates a closer collaboration than and. The ampersand is used by the Writers Guild of America to denote two writers collaborating on a specific script, rather than one writer rewriting another's work.

Why is there a double ampersand on a variable?

The double ampersand will prompt once and everywhere that variable is used it will use what was entered. If it had only one ampersand, you would get a prompt for every occurrence. Try it yourself by creating a dummy script with multiple prompts for the same variable.


3 Answers

  • The difference between the . and & operators matters only when calling PowerShell scripts or functions (or their aliases) - for cmdlets and external programs, they act the same.

  • For scripts and functions, . and & differ with respect to scoping of the definition of functions, aliases, and variables:

    • &, the call operator, executes scripts and functions in a child scope, which is the typical use case: functions and scripts are typically expected to execute without side effects:

      • The variables, (nested) functions, aliases defined in the script / function invoked are local to the invocation and go out of scope when the script exits / function returns.

      • Note, however, that even a script run in a child scope can affect the caller's environment, such as by using Set-Location to change the current location, explicitly modifying the parent scope (Set-Variable -Scope 1 ...) or the global scope ($global:...) or defining process-level environment variables.

    • ., the dot-sourcing operator, executes scripts and functions in the current scope and is typically used to modify the caller's scope by adding functions, aliases, and possibly variables for later use. For instance, this mechanism is used to load the $PROFILE file that initializes an interactive session.

The caveat is that for functions (as opposed to scripts) the reference scope for child vs. current is not necessarily the caller's scope: if the function was defined in a module, the reference scope is that module's scope domain:

  • In other words: trying to use . with a module-originated function is virtually pointless, because the scope getting modified is the module's.
  • That said, functions defined in modules aren't usually designed with dot-sourcing in mind anyway.
like image 61
mklement0 Avatar answered Oct 16 '22 08:10

mklement0


Like Mathias mentioned as a comment in this thread. & is used to invoke the expression whatever comes after the & and . is used to invoke it in the current scope and is normally used to dot source a helper file which contains functions to make it available in callers scope.

  • See https://mcpmag.com/articles/2017/02/02/exploring-dot-sourcing-in-powershell.aspx for more dot sourcing.
like image 2
Prasoon Karunan V Avatar answered Oct 16 '22 07:10

Prasoon Karunan V


You can also run things inside a module scope with the call operator, from my notes from Windows Powershell in Action.

# get and variable in module scope
$m = get-module counter
& $m Get-Variable count
& $m Set-Variable count 33

# see func def
& $m Get-Item function:Get-Count

# redefine func in memory
& $m {
  function script:Get-Count
  {
    return $script:count += $increment * 2
  }
}

# get original func def on disk
Import-Module .\counter.psm1 -Force

A few other things:

# run with commandinfo object
$d = get-command get-date
& $d

# call anonymous function
& {param($x,$y) $x+$y} 2 5

# same with dot operator
. {param($x,$y) $x+$y} 2 5
like image 2
js2010 Avatar answered Oct 16 '22 09:10

js2010