What are Dot-Source scripts in Powershell?

If the script contains functions which you need on a regular basis, then you can dot-source the script by adding a reference to it in your PowerShell Profile.

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.

By dot-sourcing the script, the script automatically loads (but not executes) when you start PowerShell. This means that you can directly type the name of the PowerShell cmdlet (function) that is contained within the script.

To dot-source a script within your powerShell profile, open PowerShell and type:

notepad $profile

If Notepad prompts you that the file doesn’t exist and whether you want to create it choose Yes. If you get an error “The system cannot find the path specified.”, close Notepad and type the following command instead:

New-Item -Path $profile -ItemType "file" -Force

Within Notepad, type a dot, then a space and then the full path to the script. For example:

. "C:\Scripts\MyScript.ps1"

Save and close Notepad and then restart PowerShell session (closing and re-opening a Powershell window, for example). You can now directly type any function contained within the MyScript.ps1 file.

Important Note: by default, your PowerShell Profile is stored in a file called Microsoft.PowerShell_profile.ps1.

To find out its exact location use the following PowerShell command:

Get-Item $profile

Example

For example, let's say we have got a script that has two functions in it. We will 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"

  }

We then have another script called DoStuff.ps1 that, among other things, needs to invoke these functions.

We could have declared these functions in the same script, but we have chosen to make the code more modular by declaring them in a separate script.

DoStuff.ps1 script might look something like this:

$thing = 'SomeThing'

  Do-Something -Thing  $thing

  Set-Something -Thing  $thing

If we invoke this script now, it will tell us 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 DoStuff.ps1 script. Let's remedy that. To make these functions available in the same scope as the calling script, we can dot-source the Functions.ps1 script inside of the DoStuff.ps1 script.

To dot source a script is incredibly simple. We will now dot source the functions inside of DoStuff.ps1 script.

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

. C:\Functions.ps1

  $thing = 'SomeThing'

  Do-Something -Thing  $thing

  Set-Something -Thing  $thing

At this point, the 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 the 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.

Dot-sourcing lots of files with just one function in each of them

What makes dot-sourcing so cool is there is no requirement to stick all of the functions in one file. You could just create one function per file and then run a simple PowerShell loop command to load all of the functions into memory. Here is an example assuming we broke out our above functions into three files.

Get-ChildItem -Path "C:\Scripts" | ForEach-Object {. $($_.FullName)}

This would import the functions of every script in the scripts folder into memory until the window was closed.