39

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

& somecommand.exe -p somearguments

However, today I came across the . operator used like this:

.$env:systemdrive\chocolatey\chocolateyinstall\chocolatey.cmd install notepadplusplus

What purpose does the period serve in this scenario? I don't get it.

Micah
  • 111,873
  • 86
  • 233
  • 325
  • Your period needs a space after it or else it will do ".full stop for an objects properties" as mentioned below. – Vippy Aug 09 '12 at 21:05
  • Correct answer below by David Brabant and commenters. This comment lodged to refute Vippy comment. 1) This . has nothing to do with object properties. 2) No space is required because $env:systemdrive is a single lexical token distinct from the . 3) a space after a leading . is only required where the . could otherwise be treated as part of a file path (typically if followed by a \\) or command name (if followed by a valid command/function/file/directory/drive name character). – Uber Kluger Jun 13 '21 at 17:17

5 Answers5

37

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. ;)

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
Vippy
  • 1,356
  • 3
  • 20
  • 30
  • In a nutshell, it's ALL about "scope". Go to Google box and search for "powershell variable scope". – Vippy Aug 09 '12 at 21:03
16

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.

codeRetard
  • 161
  • 1
  • 3
6

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.

David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • 7
    The term Microsoft coined for this is ["Dot Sourcing"](http://technet.microsoft.com/en-us/library/ee176949.aspx#ECAA). – Filburt May 23 '12 at 20:15
  • 4
    . or & makes no difference for a CMD file since it executes in a separate process (cmd.exe). – Keith Hill May 24 '12 at 00:15
  • The idea of dot sourcing like this is to change the scope so that variables created in a script are available globally. Like Keith said though, cmd scripts don't have access to that scope of variables anyway, so this doesn't really change anything. – Jason Kelley Apr 07 '14 at 15:06
  • 1
    @Filburt Unfortunately the link is outdated. Maybe [about_Scopes](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes) matches best. MS calls it "Dot Source Notation" there. – Roi Danton Nov 03 '17 at 13:07
2

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

$CompSys.TotalPhysicalMemory

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

Rahul
  • 76,197
  • 13
  • 71
  • 125
1

This answer is to expand slightly upon those already provided by David Brabant and his commenters. While those remarks are all true and pertinent, there is something that has been missed.

The OPs use of & when invoking external commands is unnecessary. Omitting the & would have no effect (on the example of his usage). The purpose of & is to allow the invocation of commands whose names are the values of a (string) expression. By using the & above, powershell then (essentially) treats the subsequent arguments as strings, the first of which is the command name that & duly invokes. If the & were omitted, powershell would take the first item on the line as the command to execute.

However, the . in the second example is necessary (although, as noted by others, & would work just as well in this case). Without it, the command line would begin with a variable access ($env:systemdrive) and so powershell would be expecting an expression of some form. However, immediately following the variable reference is a bare file path which is not a valid expression and will generate an error. By using the . (or &) at the beginning of the line, it is now treated as a command (because the beginning doesn't look like a valid expression) and the arguments are processed as expandable strings (" "). Thus, the command line is treated as

. "$env:systemdrive\chocolatey\chocolateyinstall\chocolatey.cmd" "install" "notepadplusplus"

The first argument has $env:systemdrive substituted into it and then . invokes the program thus named.

Note: the full description of how powershell processes command line arguments is way more complicated than that given here. This version was cut down to just the essential bits needed to answer the question. Take a look at about_Parsing for a comprehensive description. It is not complete but should cover most normal usage. There are other posts on stackoverflow and github (where powershell now resides) that cover some of the seemingly quirky behaviour not listed in the official documentation. Another useful resource is about_Operators though again this isn't quite complete. An example being the equivalence of . and & when invoking something other than a powershell script/cmdlet getting no mention at all.

Uber Kluger
  • 394
  • 1
  • 11