1

Every time I open a new powershell console and import-module a library (e.g AWSPowershell - yes it is old/big but I can’t use the new one without downloading it) it takes some time to fully import the module.

As soon as I close the window and open a new console, I have to repeat the wait.

I have tried examples such as adding the import to profile but this does not remove the wait time. Instead it means every time I open a powershell window there will be an automatic delay before I can start entering commands

Is there a way where I can perform a one-time import where I have access to the module regardless of closing and opening windows (whether permanent or needing to do once every time the server is restarted) ?

1
  • 1
    No, you can't persist a module import beyond the lifetime of the hosting application's process Commented May 8 at 12:42

1 Answer 1

1
  • As Mathias notes in a comment, you cannot keep modules loaded across multiple PowerShell sessions - loading modules is invariably scoped to a given session (process).

    • Hypothetically, you could create a hidden background PowerShell process that persists for the entire OS user session, which individual foreground PowerShell sessions could communicate with - analogous to how PowerShell background jobs work - but doing so isn't worthwhile, as the necessary IPC (inter-process communication) negatively impacts both performance and type fidelity.
  • Therefore, I suggest the following approach:

    • Create a shortcut file (.lnk) in your Startup folder that asynchronously launches a minimized PowerShell session and imports the module of interest every time you log on.

    • Use this session whenever you need access to the module of interest, and keep it open.


The following code creates such a shortcut file, with custom title AWS PowerShell; consider customizing the shortcut file further, e.g. with distinct colors (use Start-Process shell:startup to open the folder in File Explorer):

$shortcutFilePath = 
  Join-Path ([System.Environment]::GetFolderPath('Startup')) 'AwsPowerShell.lnk'

$shortcut =
  (New-Object -ComObject 'Wscript.Shell').CreateShortcut($shortcutFilePath)

$shortcut.TargetPath = 'powershell.exe'
$shortcut.Arguments =
  '-NoExit -c [Console]::Title = \"AWS PowerShell\"; Import-Module AWSPowerShell'
$shortcut.WindowStyle = 7 # minimize the window and don't activate it.
$shortcut.Save()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.