PowerShell

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.

This post was last modified on 3 listopada, 2018 13:10

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 miesiące ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

3 miesiące ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 miesięcy ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

12 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 rok ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 rok ago