Skip to content

Error when starting powershell 7.4, 7.5 or 7.6; "Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter “startIndex”)" #26536

Closed

Description

@segore

Prerequisites

Steps to reproduce

I am experiencing an issue with Powershell 7.4, 7.5, and 7.6. Whenever I start the shell, the following error occurs:

PowerShell 7.4.13
The shell cannot be started. A failure occurred during initialisation:
Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter “startIndex”)

This problem does not occur in version 7.3. However, Windows 11 now always pushes 7.4 on me, which I unfortunately cannot use.

I have already tried using -NoProfile and deleted the profile, but nothing has helped.

Unfortunately, I have not been able to find anything helpful regarding this issue. Does anyone have any ideas?

Many thanks in advance.

Expected behavior

The shell starts as expected without an error.

Actual behavior

The shell cannot be started. A failure occurred during initialisation:
Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter “startIndex”)

Error details

Environment data

Cannot deliver that information becuase the shell doesnt start.

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 27, 2025
  2. daxian-dbw commented on Dec 1, 2025

    @daxian-dbw
    Member

    segore I'm not aware of any similar reports, so it's likely related to the configuration or environment of your computer. Is there a stack trace shown up along with the error message by any chance? Can you share a screenshot of the issue?

  3. iSazonov commented on Dec 2, 2025

    @iSazonov
    Collaborator

    I see ConsoleHostStrings.ShellCannotBeStarted error string in ConsoleHost.cs is used 3 times.

  4. segore commented on Dec 2, 2025

    @segore
    Author

    @segore I'm not aware of any similar reports, so it's likely related to the configuration or environment of your computer. Is there a stack trace shown up along with the error message by any chance? Can you share a screenshot of the issue?

    Image
  5. daxian-dbw commented on Dec 3, 2025

    @daxian-dbw
    Member

    segore I made some changes to the error reporting code in #26565 for the ShellCannotBeStarted error, to make it print the stack trace. The CI produced and uploaded a build.zip package with the changes at https://github.com/PowerShell/PowerShell/actions/runs/19878066636/job/56969938618#step:3:411

    Artifact build.zip successfully finalized. Artifact ID 4745006649
    Artifact build has been successfully uploaded! Final size is 124159963 bytes. Artifact ID is 4745006649
    Artifact download URL: https://github.com/PowerShell/PowerShell/actions/runs/19878066636/artifacts/4745006649

    Can you please download the build.zip package, run the pwsh.exe from it, and share the stack track back here?

    • Before unzip the package, right click on it, select Properties, and then check the Unblock box.
      • or, you can run Unblock-File -Path <path-to-build.zip> from Windows PowerShell v5.1 to unblock it.
    • Then, unzip the package (if there is another build.zip file in it, unzip that one too). You will see a publish folder containing pwsh.exe. Please run this pwsh.exe the same way as you run the v7.4.13 and share the reported error back here.
  6. segore commented on Dec 3, 2025

    @segore
    Author

    @segore I made some changes to the error reporting code in #26565 for the ShellCannotBeStarted error, to make it print the stack trace. The CI produced and uploaded a build.zip package with the changes at https://github.com/PowerShell/PowerShell/actions/runs/19878066636/job/56969938618#step:3:411

    Artifact build.zip successfully finalized. Artifact ID 4745006649
    Artifact build has been successfully uploaded! Final size is 124159963 bytes. Artifact ID is 4745006649
    Artifact download URL: https://github.com/PowerShell/PowerShell/actions/runs/19878066636/artifacts/4745006649

    Can you please download the build.zip package, run the pwsh.exe from it, and share the stack track back here?

    • Before unzip the package, right click on it, select Properties, and then check the Unblock box.

      • or, you can run Unblock-File -Path <path-to-build.zip> from Windows PowerShell v5.1 to unblock it.
    • Then, unzip the package (if there is another build.zip file in it, unzip that one too). You will see a publish folder containing pwsh.exe. Please run this pwsh.exe the same way as you run the v7.4.13 and share the reported error back here.

    Thank you very much for your help. Here is the stacktrace:

    PowerShell 7.6.0-preview.6
    at System.String.IndexOf(Char value, Int32 startIndex, Int32 count)
    at System.Management.Automation.ModuleIntrinsics.UpdatePath(String path, String pathToAdd, Int32& insertIndex) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1277
    at System.Management.Automation.ModuleIntrinsics.GetModulePath(String currentProcessModulePath, String hklmMachineModulePath, String hkcuUserModulePath) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1267
    at System.Management.Automation.ModuleIntrinsics.SetModulePath() in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1370
    at System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext context) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 47
    at System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\ExecutionContext.cs:line 1675
    at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, InitialSessionState iss) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\AutomationEngine.cs:line 65
    at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper() in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\hostifaces\LocalConnection.cs:line 646
    at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\hostifaces\ConnectionBase.cs:line 288
    at Microsoft.PowerShell.ConsoleHost.OpenConsoleRunspace(Runspace runspace, Boolean staMode) in D:\a\PowerShell\PowerShell\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHost.cs:line 1823
    at Microsoft.PowerShell.ConsoleHost.DoCreateRunspace(RunspaceCreationEventArgs args) in D:\a\PowerShell\PowerShell\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHost.cs:line 1766

    Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')

    Image
  7. daxian-dbw commented on Dec 4, 2025

    @daxian-dbw
    Member

    segore Thank you for helping pinpoint the error.

    Now I know where the error happened, but I still need some extra information to understand why it happened. I pushed another commit to add some debugging messages in #26565, and the CI produced another build.zip package at https://github.com/PowerShell/PowerShell/actions/runs/19913053584/job/57085927902?pr=26565#step:3:399

    Artifact build.zip successfully finalized. Artifact ID 4758243853
    Artifact build has been successfully uploaded! Final size is 124159682 bytes. Artifact ID is 4758243853
    Artifact download URL: https://github.com/PowerShell/PowerShell/actions/runs/19913053584/artifacts/4758243853

    Can you please try out the new build.zip package produced in CI and share the output back?

    • Unblock the zip package before unzipping it.
    • Open CMD, run set _debug_me_=1 to set the environment variable _debug_me_
    • Start pwsh.exe unzipped from the package in the same CMD window.
    • Share the output back here.
  8. segore commented on Dec 4, 2025

    @segore
    Author
    • pwsh.exe

    @segore Thank you for helping pinpoint the error.

    Now I know where the error happened, but I still need some extra information to understand why it happened. I pushed another commit to add some debugging messages in #26565, and the CI produced another build.zip package at https://github.com/PowerShell/PowerShell/actions/runs/19913053584/job/57085927902?pr=26565#step:3:399

    Artifact build.zip successfully finalized. Artifact ID 4758243853
    Artifact build has been successfully uploaded! Final size is 124159682 bytes. Artifact ID is 4758243853
    Artifact download URL: https://github.com/PowerShell/PowerShell/actions/runs/19913053584/artifacts/4758243853

    Can you please try out the new build.zip package produced in CI and share the output back?

    • Unblock the zip package before unzipping it.
    • Open CMD, run set _debug_me_=1 to set the environment variable _debug_me_
    • Start pwsh.exe unzipped from the package in the same CMD window.
    • Share the output back here.

    PowerShell 7.6.0-preview.6
    ---- UpdatePath ----
    path: ;
    pathToAdd: C:\Users\user\OneDrive - company &amp; name namepart2\Dokumente\PowerShell\Modules
    insertIndex: 0
    -------- END --------
    ---- UpdatePath ----
    path: ;
    pathToAdd: C:\Users\user\OneDrive - company &amp; name namepart2\Dokumente\PowerShell\Modules
    insertIndex: 0
    -------- END --------
    at System.String.IndexOf(Char value, Int32 startIndex, Int32 count)
    at System.Management.Automation.ModuleIntrinsics.UpdatePath(String path, String pathToAdd, Int32& insertIndex) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1286
    at System.Management.Automation.ModuleIntrinsics.GetModulePath(String currentProcessModulePath, String hklmMachineModulePath, String hkcuUserModulePath) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1267
    at System.Management.Automation.ModuleIntrinsics.SetModulePath() in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 1379
    at System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext context) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\Modules\ModuleIntrinsics.cs:line 47
    at System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\ExecutionContext.cs:line 1675
    at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, InitialSessionState iss) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\AutomationEngine.cs:line 65
    at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper() in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\hostifaces\LocalConnection.cs:line 646
    at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall) in D:\a\PowerShell\PowerShell\src\System.Management.Automation\engine\hostifaces\ConnectionBase.cs:line 288
    at Microsoft.PowerShell.ConsoleHost.OpenConsoleRunspace(Runspace runspace, Boolean staMode) in D:\a\PowerShell\PowerShell\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHost.cs:line 1823
    at Microsoft.PowerShell.ConsoleHost.DoCreateRunspace(RunspaceCreationEventArgs args) in D:\a\PowerShell\PowerShell\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHost.cs:line 1766

    Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')

    I have anonymised the path mentioned above because I can now imagine the direction in which the problem might be heading. Do we have a problem here with the encoded & and the resulting ‘;’?

  9. daxian-dbw commented on Dec 9, 2025

    @daxian-dbw
    Member

    Do we have a problem here with the encoded & and the resulting ‘;’?

    Yes, the resulting ; from the encoded & caused the single path to be treated as 2 paths separated by the Path.PathSeparator, which is unexpected and led to the exception.

    The path C:\Users\marcv\OneDrive - company &amp; name namepart2\Dokumente\PowerShell\Modules is the personal module path constructed by Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) and it's supposed to be a single path.

    I have no idea why your company OneDrive path has the HTML encoded & in it. Can you change it? It's fine to use & directly in a folder name.

  10. segore commented on Dec 9, 2025

    @segore
    Author

    Do we have a problem here with the encoded & and the resulting ‘;’?

    Yes, the resulting ; from the encoded & caused the single path to be treated as 2 paths separated by the Path.PathSeparator, which is unexpected and led to the exception.

    The path C:\Users\marcv\OneDrive - company &amp; name namepart2\Dokumente\PowerShell\Modules is the personal module path constructed by Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) and it's supposed to be a single path.

    I have no idea why your company OneDrive path has the HTML encoded & in it. Can you change it? It's fine to use & directly in a folder name.

    I don't know why we have the encoded ampersand in the company name either, probably for historical reasons, or as a result of the AD => AAD/EntryID migration.

    But I was able to reconfigure OneDrive and assign new folders, so fortunately the error no longer occurs. Now powershell starts without a problem :-)

    Thank you very much for your help. I hadn't noticed the ; in the path as a possible cause before the debug output.

  11. iSazonov commented on Dec 9, 2025

    @iSazonov
    Collaborator

    We still need to return a useful message instead of a crash.

  12. daxian-dbw commented on Dec 10, 2025

    @daxian-dbw
    Member

    Yes, there are 2 issues that we need to address:

    1. The error reporting in the method ReportExceptionFallback(Exception e, string header) does not write out stack trace of the exception, which makes it hard to diagnose failures like ShellCannotBeStarted.
    2. The UpdatePath method used in module path construction doesn't handle the case where personalModulePathToUse may contain multiple paths separated by the PathSperator (user can define PSModulePath in powershell.config.json with such a value).

    The PR with fixes was submitted: #26565

  13. removed
    Needs-Triage
    on Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions