locked
Broadcasting PSModulePath RRS feed

  • Question

  • Hello

    We have installation wizard to update the PSModulePath (add/remove path) in the Registry key

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PSModulePathAfter that the installation calls this script to broadcast the change:

    Add-Type -TypeDefinition @"
        using System;
        using System.Runtime.InteropServices;

        public class NativeMethods
        {
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessageTimeout(
                IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
                uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
        }
    "@

    $HWND_BROADCAST = [IntPtr] 0xffff
    $WM_SETTINGCHANGE = 0x1a
    $SMTO_ABORTIFHUNG = 0x2
    $result = [UIntPtr]::Zero

    [void] ([Nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, 'Environment', $SMTO_ABORTIFHUNG, 5000, [ref] $result))

    However, the PSModulePath is not changed in the PS console that was open before installation begins and completes.

    It does not sound like the behavior described in this paragraph from

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx

    "...

    Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

    ..."

    Can someone clarify?

    Thanks in advance.

    Monday, June 19, 2017 7:42 AM

All replies

  • If the program does not detect the notifications then it will not see the updates.  PS console is not a GUI app.  It does not see this message.

    \_(ツ)_/

    • Proposed as answer by Hello_2018 Thursday, June 22, 2017 7:02 AM
    Monday, June 19, 2017 6:39 PM
  • Hi Andy,

    Thanks for answer. I assume you are right and there is no workaround for any running PS console before the installation modifies the PSModulePath in the Environment reg key for the system hive.

    I could verify that answer for a sample GUI app to notice the change if the Broadcast script is called. However, this is irrelevant for the PS console.

    Anyway, it seems to me that it is a good answer about the script to broadcast the Env change.

    Based on your answer, the end user shoul;d open a new PS console after the installation completes in order that new modified PSModulePath to be taken in consideration. Right?

    Thanks for answer.

    Thursday, June 22, 2017 8:15 AM
  • Hi Andy,

    Thanks for answer. I assume you are right and there is no workaround for any running PS console before the installation modifies the PSModulePath in the Environment reg key for the system hive.

    I could verify that answer for a sample GUI app to notice the change if the Broadcast script is called. However, this is irrelevant for the PS console.

    Anyway, it seems to me that it is a good answer about the script to broadcast the Env change.

    Based on your answer, the end user shoul;d open a new PS console after the installation completes in order that new modified PSModulePath to be taken in consideration. Right?

    Thanks for answer.

    I suppose you should thanks JRV.

    I'm only agree with what she/he said.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, June 23, 2017 5:23 AM