Set Path Variable with PowerShell

Set Path Variable with PowerShell

Hi All,

While upgrading my PowerShell Module from the PowerShell Gallery, i’ve got a Warning in PowerShell7 that the Path for the Scripts is missing in my Path Variable. In PowerShell 5.1 it worked already fine.

Uninstall-PSResource -Name Get-Mailprotection -Scope CurrentUser
Install-PSResource -Name Get-Mailprotection -Scope CurrentUser

The Path Variable can be found in the System Properties > “Environment Variables”

As you can see there are “User Variables” and “System Variables”

Let’s list the Path Variable with PowerShell

#Get Path User Variables
[Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) -split ";"

#Get Path System Variables
[Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) -split ";"

Update the Path Variable un the Users Variables

#Get Path Variable from Current User
$ArrayPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) -split ";"

#Add Path 
$AddPath = "C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Scripts"
$ArrayPath += $AddPath

#Remove Empty Lines
$NewArrayPath = ($ArrayPath -split ";" | where {$_ -ne ""}) -join ";"

#Set Path Variable from Current User
[Environment]::SetEnvironmentVariable("Path", $NewArrayPath, [System.EnvironmentVariableTarget]::User)

#Get Path User Variables
[Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) -split ";"

Some of you might know the $Env:Path - this will give you all the Path Variables from User and System combined

$env:Path -split ";"

Now the Script can be executed

Get-Mailprotection -Domain icewolf.ch

Regards
Andres Bohren

PowerShell Logo