#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

Name
----
__Set-Prompt.ps1
Credential.ps1
Get-EnumValue.ps1
Get-ExceptionDescription.ps1
Get-Menu.ps1
…

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.

About the author: David Moravec

I am in IT industry for 15 years, last 10 years working mainly with Microsoft management technologies. I Work in one of the biggest international accounting companies as administrator responsible for System Center Configuration Manager. I like to automate my infrastructure-related tasks with windows PowerShell. Author and speaker for Microsoft Czech Republic. I'm Running my own blog at PowerShell.cz. You can reach me on Twitter at @makovec

Related Posts

%d bloggers like this: