#PSTip How to automatically dot-source all scripts in a folder

As I mentioned in my previous post, I like to have access to (almost) all my scripts on all machines. That’s why I have Scripts folder inside my Dropbox:\PowerShell\Profile. Anytime I have some nice script I drop it there and can use it.

# Load all scripts
Get-ChildItem (Join-Path ('Dropbox:\PowerShell\Profile') \Scripts\) | Where <code>
    { $_.Name -notlike '__*' -and $_.Name -like '*.ps1'} | ForEach </code>
    { . $_.FullName }

I am just running dir against Scripts folder and then check if the file has the .ps1 extension, but doesn’t start with “__” (double underscore). A reason? If I want to temporarily remove just one script from loading I will just rename it and prefix its name with a “__”. So, from the following list:

[23]: dir Dropbox:\PowerShell\Profile\Scripts | Sort Name | Select Name


The first script is not processed. The other scripts are piped to another command where I dot-source them. I also use auto-loading of my own format files.

# Load format files
Get-ChildItem (Join-Path ('Dropbox:\PowerShell\Profile\Scripts') \Format\) | ForEach `
    { Update-FormatData -AppendPath $_.FullName }

I use these format files to correctly format output from Configuration Manager WMI provider for some of my own scripts.

Filed in: Columns, Tips and Tricks Tags: , ,

2 Responses to "#PSTip How to automatically dot-source all scripts in a folder"

  1. Johnny Leuthard says:

    This is a great tip/idea. I was thinking of writing a script that everyone on a team could run to download and update a set of custom Cmdlets. This way I can just drop it in a folder and they will have it next time they restart the shell.


  2. Alexandru says:

    Awesome, thanks!
    You’d think dot sourcing multiple script files via wildcards should work but nah..

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: