This is a test website to try out GitHub and Git Bash
19 Jan 2025 - jhunter
help about_Operators -ShowWindow
This blog shows a couple of ways to use dots .
and ampersands &
in PowerShell.
When writing the path to a file, begin the path like this to show that the item is inside the present working directory.
.\
For example:
get-childitem .\
Run a script in the current scope.
. C:\Temp\20250119_test.ps1
You can use the dot sourcing operator as an alias of ìmport-module
, when the .ps1 file contains functions.
The ampersand is a call operator. When you call a script file with the ampersand the code is run in a different scope to the curent one.
& C:\Temp\20250119_test.ps1
Since the script gets executed in another scope, it doesn’t make sense to use an ampersand to import a function.
I create this simple function inside the file C:\Temp\20250119_test.txt.
function james-test{
$x = 1
return $x
}
Trying to import the script with an ampersand fails. With the dot source operator it works.
Once the function is imported, the $x
variable is still null.
PS C:\Temp> & .\20250119_test.ps1
PS C:\Temp> dir function:*james*
PS C:\Temp>
PS C:\Temp> $x
PS C:\Temp>
PS C:\Temp> . .\20250119_test.ps1
PS C:\Temp> dir function:*james*
CommandType Name Version Source
----------- ---- ------- ------
Function james-test
PS C:\Temp> $x
PS C:\Temp>
When I run the function with an ampersand the script runs in another scope and returns the value of $x
. However, the value of $x
in the current scope is unchanged.
Interestingly, if I run the function without any call operator, it is as if I used the ampersand. The function is executed but the value of $x
is unchanged in the current scope.
The value of $x
is changed if I run the function in the current scope with a dot.
PS C:\Temp> & james-test
1
PS C:\Temp> $x
PS C:\Temp>
PS C:\Temp> james-test
1
PS C:\Temp> $x
PS C:\Temp>
PS C:\Temp>
PS C:\Temp> . james-test
1
PS C:\Temp> $x
1
PS C:\Temp>