#PSTip Refresh the PSModulePath environment variable without re-opening console

A co-worker had asked me a question about why the SQLPS module was not available for importing immediately after an automated install of SQL using a PowerShell script. Within this script, he was installing SQL as step 1 and then in step 2, he needs to use SQLPS module for setting some SQL configuration. However, when the script came to step 2, it complained that the SQLPS module was not found.

If you have worked on SQL PowerShell module, you may be aware of the fact that they store the module in Program Files folder for SQL Server and they update the PSModulePath system environment variable. But, this change won’t be available to PowerShell unless you reopen the PowerShell console. In a sequential execution flow, this is not an option.

Here is how we solved the problem:

$env:PSModulePath = [System.Environment]::GetEnvironmentVariable("PSModulePath","Machine")
Filed in: Columns, Tips and Tricks Tags: ,

3 Responses to "#PSTip Refresh the PSModulePath environment variable without re-opening console"

  1. cd says:

    so simple; yet so effective. Thanks for sharing.

  2. Phillip Cox says:

    Totally agree, very simple and effective.

    Thanks for sharing!

  3. Attila says:

    Thanks for sharing. Works well also when Skype for Business is beeing installed with prepared commands.

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: