#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")
Share on: