Reputation: 261
I'm working on a PowerShell script to install the Oracle client 19c. One of the things I need to do in the script before the install starts is to remove an environment variable from the Windows registry. This is the command I am using in the script to do this:
Remove-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "ORACLE_BASE" -Force
The problem is when the install starts later in the script it still thinks that setting is there, even though after the script is done running the setting is no longer there when I look. So I'm thinking I need to run that command in a special instance or something so it takes effect right away. Just not sure the best way to do that. I've tried using both start-process and invoke-expression to launch powershell within the script and neither method seems to do the trick. Any help would be much appreciated!
Upvotes: 0
Views: 85
Reputation: 24585
The problem is when the install starts later in the script it still thinks that setting is there, even though after the script is done running the setting is no longer there when I look. So I'm thinking I need to run that command in a special instance or something so it takes effect right away.
Your assumption is not correct.
Environment variables for an executable are inherited from the process that started the executable. Removing the environment variable from the registry will not make it disappear from the current process. This is why "it still thinks the setting is there."
If you really need the variable not to be present in the environment when starting the install, then unset it from a shell and run the installer:
cmd.exe
shell script/batch:
set ORACLE_BASE=
PowerShell:
Remove-Item Env:ORACLE_BASE -ErrorAction SilentlyContinue
-ErrorAction SilentlyContinue
can be shortened to -EA 0
(it prevents an error message if the variable isn't in the environment).
After removing the variable (use the command above that matches your shell), then start the installer from that shell. Then the installer inherits the environment from the shell, and thus the variable will not be defined in the installer's process.
Upvotes: 2