Artykuł

PowerShell – Environment Path is missing or overwritten

I had a strange issue today when I was doing some development where suddenly my scripts would report inability to find some of my modules. Upon short investigation I found out $Env:PSModulePath is missing „C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules”

As I've done only one thing – installing an update to AutoIt v3, it must have been during the install process that this value gets overwritten by AutoIt v3 installer. Seeing how C:\Program Files (x86)\AutoIt3\AutoItX is the only path displayed when checking [Environment]::GetEnvironmentVariable(„PSModulePath”, „Machine”) variable I'm reasonably sure AutoIt is at fault here. That means there are only two things to do. One is to report an issue to AutoIt to fix their Installer. Second is to set my $Env:PSModulePath to a proper value.

What is the problem?

By default following paths are defined on Windows 10

In my case it's only showing one value

How to fix this?

Following code provides a 3 step setup. First, it gets current value and displays it, and then it updates its, finally it shows you updated value. Feel free to do it in steps rather than one go.

$MissingModule = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules"

# Display Current Value
$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$CurrentValue

# Fix
[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";$MissingModule", "Machine")

# Display Updated Value
$UpdatedValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$UpdatedValue

Since AutoIt also overwritten other path C:\Program Files\WindowsPowerShell\Modules we need to rerun this twice with a different path. Running below code should give me my two missing paths along with AutoIt v3 PowerShell Module path.

$UpdatedValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$UpdatedValue -split ';'

That's it. If you reopen PowerShell session, you should have all paths available now, and all modules should work again.

Tags: , , , , , ,

This is a unique website which will require a more modern browser to work! Please upgrade today!