What is dot sourcing in PowerShell?

Understand dot sourcing in PowerShell

John Savill

February 25, 2017

1 Min Read
What is dot sourcing in PowerShell?

Q. What is dot sourcing in PowerShell?

A. Ordinarily when a script is executed it can be run as

.script.ps1

This creates a child scope for the script however once the script completes any objects create are discarded, for example any variables created.

Alternatively you can dot source the script which will run in the current scope which means any variables etc will be maintained post script execution, e.g.

. .script.ps1

You can easily test this. Save the following as script.ps1:

$answer="42"write-output "ultimate answer is $answer"

Now see example execution:

PS D:temp> .script.ps1ultimate answer is 42PS D:temp> $answerPS D:temp> . .script.ps1ultimate answer is 42PS D:temp> $answer42

 

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like