PowerShell How-To

Exploring Dot-Sourcing in PowerShell

Dot-sourcing allows for you to keep your functions modular.

When writing PowerShell scripts, there always seems to come a time when a single script just isn't enough. Although it's important to keep your scripts as simple as possible, it's also important to keep your code modular. Scripts and functions should do one "thing." When a need arises to add an additional "thing" that's when it's time to add another script or function to your project. Dot-sourcing is a way to do just that.

Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script in a separate one. This is useful when you're working on a project with multiple scripts and might have functions specified in one script(s) and the code to call those functions in another. Other than building a module, dot-sourcing is a good way to make that code in another script available to you.

For example, let's say I've got a script that has two functions in it. We'll call this script Functions.ps1.

function Do-Something {
Write-Output "I did something to $Thing"

function Set-Something {
Write-Output "I set something on $Thing"

I then have another script called DoStuff.ps1 that, among other things, needs to invoke these functions. I could have declared these functions in the same script, but I've chosen to make my code more modular by declaring them in a separate script.

My DoStuff.ps1 script might look something like this:

$thing = 'SomeThing'
Do-Something -Thing $thing
Set-Something -Thing $thing

If I invoke this script now, it will tell me that the commands Do-Something and Set-Something are not available. This is because these functions are not loaded in the same session scope as my DoStuff.ps1 script. Let's remedy that. To make these functions available in the same scope as my calling script, I can dot-source the Functions.ps1 script inside of the DoStuff.ps1 script. To dot source a script is incredibly simple. I'll now dot source my functions script inside of my DoStuff.ps1 script.

If my Functions.ps1 script were located on the root of my C drive, I just put a period, followed by a space and then the path of the script I'd like to dot source.

. C:\Functions.ps1
$thing = 'SomeThing'
Do-Something -Thing $thing
Set-Something -Thing $thing

At this point, my DoStuff.ps1 script will execute as expected. This is because the script containing the function declarations for the Do-Something and Set-Something functions have now been loaded into the same session scope as my calling script.

One other thing to note is then when dot sourcing, do not include procedural code inside of the script being dot-sourced. Procedural code is any code not in a function. If you do, you'll find that the dot-sourced script will actually execute that code which is typically not what we're after.

That's all there is to dot sourcing. However, when thinking about dot sourcing a script, look into creating a module instead. Both solve the same problem, but a module allows more flexibility and easier management. However, if all that's needed is a quick way to bring one script into another script's scope, dot sourcing is the way to go.

About the Author

Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.