Using Dot Sourcing to Test Locally with PowerShell

Various technologies have silly names for things, although in some rare cases the names are directly correlated to an activity. While delivering a number of PowerShell and RESTful API sessions at VMware User Groups, I used the term dot sourcing to describe a quick testing activity for scripts. Afterwards, I received questions on what this meant – meaning I took the knowledge for granted. This “curse of knowledge” can really bite me in the pants at times, so I figured it’d be worth writing down what dot sourcing is and how it works in a blog post. Plus, it’s easier to reference in the future. 🙂

In my case, I’m talking about the use of a specific syntax within PowerShell to “informally” load a script. I often use it to load a specific script contained within a module, such as the one I’m contributing to for Rubrik, without having to reload the entire module. The syntax is:

. ./path/to/script.ps1

This activity will cause the script to become available in the scope of my PowerShell console, which is handy for a number of reasons:

  1. Pass arguments to a script using Intellisense / Autocomplete to tab through the options (I’m lazy).
  2. Include the script within another script or function to validate any changes made to the code (trying new things).
  3. More realistic testing of the script without worrying about permanent placement of the file (related to trying new things).

To demonstrate this, I’ll pull up a list of PowerShell scripts from my local copy of the Rubrik module. I’ve made a change to the Connect-Rubrik.ps1 file and now want to test it locally. If I unload the module and then try to run the script, the console’s scope has no idea what I’m referring to, which has been demonstrated below:

connect-rubrik-no-dot-source

However, using the dot sourcing technique, I can temporarily load the script for testing.

. .\Connect-Rubrik.ps1
connect-rubrik-with-dot-sourcing

This is a handy little trick for tinkering around with PowerShell scripts on your local machine without having to rely upon more formal installation or deployment methods. Sometimes I think it’s so simple that it doesn’t get enough attention or detailed instructions. I hope this helps with your coding – enjoy!