I have a few modules in my powershell profile, and their loading sometimes takes a lot of time (10-15s). I would like to load them in a background thread using something like this:
Start-Job -ScriptBlock { Import-Module -Global DockerCompletion }
The problem is that even though the documentation states that Import-Module uses global scope as a default, when I use Get-Module in the parent shell the module is not listed, so it was not loaded even though the job states completed.
Some of modules I use:
Import-Module “$env:ChocolateyInstall\helpers\chocolateyProfile.psm1” -Force
Import-Module WSLTabCompletion
Import-Module posh-git
Import-Module npm-completion
Import-Module DockerCompletion
EDIT: I am using pwsh 7.1
$PsModulePath
, which could include your custom path), PowerShell should do this as required: Implicitly Importing a Module – ApprehendStart-Job
runs the background job in a separate process, so doesn't affect the global scope of the calling session. – Toluate[System.Threading.Thread]::Start
, but I don't have any ready examples or code to provide as an answer here. – ExocarpRegister-ArgumentCompleter
to generate and add various tab-completion behaviors rather than functions/cmdlets. I think you cannot do this in the background either since they need to register in the current context. – InkberryPSThreadJob
(open source module for 5.1 thread jobs) and the officialThreadJob
module for PS Core, neither seem to be able to import a module where it is usable from the invoking session, at least not by default. – ExocarpGet-Module
to see which modules were loaded in the current session). To be fair, I have not gone the full mile of inspection to see what members are available from the threadjob objects, but at a glance it doesn't seem like a background thread will work for importing modules. – ExocarpSystem.Threading
namespace directly. – Exocarp$env:PSModulePath
– Shophar$env:PSModulePath -split ';'
– Shophar