Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Utils.cs (C#)

Code before:
                    s_windowsPowerShellVersion = engineKey.GetValue(RegistryStrings.MonadEngine_MonadVersion) as string;
                    return s_windowsPowerShellVersion;
                }
            }

            return string.Empty;
        }
#endif

        internal static string DefaultPowerShellAppBase => GetApplicationBase(DefaultPowerShellShellID);

        internal static string GetApplicationBase(string shellId)
        {
            var baseDirectory = AppContext.BaseDirectory;
            if (!string.IsNullOrEmpty(baseDirectory))
            {
                // need to remove any trailing directory separator characters
                baseDirectory = baseDirectory.TrimEnd(Path.DirectorySeparatorChar);
            }
            else
            {
                // host path can be empty if hosted in WinRM, so fallback to using SMA.dll location
                baseDirectory = Path.GetDirectoryName(typeof(PSObject).Assembly.Location);
            }

            return baseDirectory;
        }

        private static string[] s_productFolderDirectories;

        private static string[] GetProductFolderDirectories()
        {
            if (s_productFolderDirectories == null)
            {
                List<string> baseDirectories = new List<string>();

                // Retrieve the application base from the registry
                string appBase = Utils.DefaultPowerShellAppBase;
                if (!string.IsNullOrEmpty(appBase))
                {
                    baseDirectories.Add(appBase);
                }
#if !UNIX
                // Now add the two variations of System32
                baseDirectories.Add(Environment.GetFolderPath(Environment.SpecialFolder.System));
                string systemX86 = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
                if (!string.IsNullOrEmpty(systemX86))
                {
                    baseDirectories.Add(systemX86);
                }
#endif

Review comment (suggestion):
[Current AppContext.BaseDirectory implementation](https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/AppContext.AnyOS.cs,24) return `Assembly.GetEntryAssembly()?.Location` for not AOT (for pwsh.exe it is `pwsh.dll` location). It can be not that we want in some scenarios. Also do we really need `APP_CONTEXT_BASE_DIRECTORY`?
I think logic could be:
```c#
var baseDirectory = Path.GetDirectoryName(typeof(PSObject).Assembly.Location);
if (string.IsNullOrEmpty(baseDirectory))
{
    baseDirectory = Environment.ProcessPath;
}
```

Code after:
                    s_windowsPowerShellVersion = engineKey.GetValue(RegistryStrings.MonadEngine_MonadVersion) as string;
                    return s_windowsPowerShellVersion;
                }
            }

            return string.Empty;
        }
#endif

        internal static string DefaultPowerShellAppBase => GetApplicationBase(DefaultPowerShellShellID);

        internal static string GetApplicationBase(string shellId)
        {
            // Use the location of SMA.dll as the application base if it exists,
            // otherwise, use the base directory from `AppContext`.
            var baseDirectory = Path.GetDirectoryName(typeof(PSObject).Assembly.Location);
            if (string.IsNullOrEmpty(baseDirectory))
            {
                // Need to remove any trailing directory separator characters
                baseDirectory = AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
            }

            return baseDirectory;
        }

        private static string[] s_productFolderDirectories;

        private static string[] GetProductFolderDirectories()
        {
            if (s_productFolderDirectories == null)
            {
                List<string> baseDirectories = new List<string>();

                // Retrieve the application base from the registry
                string appBase = Utils.DefaultPowerShellAppBase;
                if (!string.IsNullOrEmpty(appBase))
                {
                    baseDirectories.Add(appBase);
                }
#if !UNIX
                // Now add the two variations of System32
                baseDirectories.Add(Environment.GetFolderPath(Environment.SpecialFolder.System));
                string systemX86 = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
                if (!string.IsNullOrEmpty(systemX86))
                {
                    baseDirectories.Add(systemX86);
                }
#endif
                Interlocked.CompareExchange(ref s_productFolderDirectories, baseDirectories.ToArray(), null);
            }