PowerShell How-To

What Is Dot-Sourcing in PowerShell?

Dot-sourcing allows you keep everything modular when adding additional scripts.

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 {
param($Thing)
Write-Output "I did something to $Thing"
}

function Set-Something {
param($Thing)
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 a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025