Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs (C#)

Code before:
            {
                while (!System.Diagnostics.Debugger.IsAttached)
                {
                    Thread.Sleep(1000);
                }
            }
#endif

            // Check for external InitialSessionState configuration conflict with '-ConfigurationFile' argument.
            if (issProvidedExternally && !string.IsNullOrEmpty(s_cpp.ConfigurationFile))
            {
                throw new ConsoleHostStartupException(ConsoleHostStrings.ShellCannotBeStartedWithConfigConflict);
            }

            // Put PSHOME in front of PATH so that calling `pwsh` within `pwsh` always starts the same running version.
            string path = Environment.GetEnvironmentVariable("PATH");
            string dotnetToolsPathSegment = Path.Combine(".dotnet", "tools", ".store");
            string pshome = Utils.DefaultPowerShellAppBase;

            int index = pshome.IndexOf(dotnetToolsPathSegment, StringComparison.Ordinal);
            if (index > 0)
            {
                // We're running PowerShell global tool. In this case the real entry executable should be the 'pwsh'
                // or 'pwsh.exe' within the '../.dotnet/tools' folder, not what PSHome is pointing to.
                // BTW, 13 is the length of '.dotnet/tools'
                pshome = pshome[0..(index + 13)];
            }

            pshome += Path.PathSeparator;

            // To not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front
            // we also don't handle the edge case where PATH only contains $PSHOME
            if (string.IsNullOrEmpty(path))
            {
                Environment.SetEnvironmentVariable("PATH", pshome);
            }
            else if (!path.StartsWith(pshome, StringComparison.Ordinal))
            {
                Environment.SetEnvironmentVariable("PATH", pshome + path);
            }

            try
            {
                string profileDir = Platform.CacheDirectory;
#if !UNIX
                if (!Directory.Exists(profileDir))
                {
                    Directory.CreateDirectory(profileDir);
                }
#endif
                ProfileOptimization.SetProfileRoot(profileDir);

Review comment (suggestion):
this is true for global tools that are installed globally with `dotnet tool install -g`. It can be installed to any path, but I agree that most people will install it globally. Maybe add this to comments.

Code after:
            {
                while (!System.Diagnostics.Debugger.IsAttached)
                {
                    Thread.Sleep(1000);
                }
            }
#endif

            // Check for external InitialSessionState configuration conflict with '-ConfigurationFile' argument.
            if (issProvidedExternally && !string.IsNullOrEmpty(s_cpp.ConfigurationFile))
            {
                throw new ConsoleHostStartupException(ConsoleHostStrings.ShellCannotBeStartedWithConfigConflict);
            }

            // Put PSHOME in front of PATH so that calling `pwsh` within `pwsh` always starts the same running version.
            string path = Environment.GetEnvironmentVariable("PATH");
            string pshome = Utils.DefaultPowerShellAppBase;
            string dotnetToolsPathSegment = $"{Path.DirectorySeparatorChar}.store{Path.DirectorySeparatorChar}powershell{Path.DirectorySeparatorChar}";

            int index = pshome.IndexOf(dotnetToolsPathSegment, StringComparison.Ordinal);
            if (index > 0)
            {
                // We're running PowerShell global tool. In this case the real entry executable should be the 'pwsh'
                // or 'pwsh.exe' within the tool folder which should be the path right before the '\.store', not what
                // PSHome is pointing to.
                pshome = pshome[0..index];
            }

            pshome += Path.PathSeparator;

            // To not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front
            // we also don't handle the edge case where PATH only contains $PSHOME
            if (string.IsNullOrEmpty(path))
            {
                Environment.SetEnvironmentVariable("PATH", pshome);
            }
            else if (!path.StartsWith(pshome, StringComparison.Ordinal))
            {
                Environment.SetEnvironmentVariable("PATH", pshome + path);
            }

            try
            {
                string profileDir = Platform.CacheDirectory;
#if !UNIX
                if (!Directory.Exists(profileDir))
                {
                    Directory.CreateDirectory(profileDir);
                }
#endif
                ProfileOptimization.SetProfileRoot(profileDir);