Skip to content

Ideally, Modules should support a Module.profile.ps1 #537

Open
@StartAutomating

Description

@StartAutomating

In PowerShell, profile scripts are used to import functionality whenever someone loads PowerShell.
Profile scripts can be distinct for each user and host, and will populate the PowerShell automatic variable $profile.

This concept can be easily extended to loading a "Module Profile". This was first described in Posh.

At the end of a module's .psm1, it should check to see if file exists in the same directory as $profile:

$MyModule = $myInvocation.MyCommand.ScriptBlock.Module
$MyModuleProfile = $profile | Split-Path | Join-Path -ChildPath "$MyModule.profile.ps1"
if (Test-Path $myModuleProfile) {
   . $MyModuleProfile
}

Using . to call the profile will include the Module Profile's functionality in the module
Using '&' to call the profile will simply run the Module Profile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions