Use Get-ChildItem to grab all *.ps1 files, then loop through them with ForEach-Object and dot-source them individually
$Path = "C:\Scripts\Directory"
Get-ChildItem -Path $Path -Filter *.ps1 |ForEach-Object {
. $_.FullName
}
If you put the above in a script, and the script itself is located in $Path, make sure to exclude the file itself so as to avoid it recursively dot-sourcing itself over and over again:
$Path = "C:\Scripts\Directory"
Get-ChildItem -Path $Path -Filter *.ps1 |Where-Object { $_.FullName -ne $PSCommandPath } |ForEach-Object {
. $_.FullName
}
Edit: jcolebrand - 2018-06-25
Used the above to define a folder called Functions under my executing script. Works just fine in PS5:
## Folder\myIncluder.ps1
## Folder\Functions\Some-Function.ps1 -included
## Folder\Functions\Some-Other-Function.ps1 -included
## Folder\Functions\Some-Other-Function.readme -not included
(Get-ChildItem -Path (Join-Path $PSScriptRoot Functions) -Filter *.ps1 -Recurse) | % {
. $_.FullName
}
Figure this makes it easy for the next lazy person to just adopt the same structure :p
. .\*.ps1worked