You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
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?
@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?
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.
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')
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 & name namepart2\Dokumente\PowerShell\Modules
insertIndex: 0
-------- END --------
---- UpdatePath ----
path: ;
pathToAdd: C:\Users\user\OneDrive - company & 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 ‘;’?
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 & 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.
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 & 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.
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.
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).
Activity
daxian-dbw commentedon Dec 1, 2025
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?
iSazonov commentedon Dec 2, 2025
I see
ConsoleHostStrings.ShellCannotBeStartederror string in ConsoleHost.cs is used 3 times.segore commentedon Dec 2, 2025
daxian-dbw commentedon Dec 3, 2025
segore I made some changes to the error reporting code in #26565 for the
ShellCannotBeStartederror, to make it print the stack trace. The CI produced and uploaded abuild.zippackage with the changes at https://github.com/PowerShell/PowerShell/actions/runs/19878066636/job/56969938618#step:3:411Can you please download the
build.zippackage, run thepwsh.exefrom it, and share the stack track back here?Properties, and then check theUnblockbox.Unblock-File -Path <path-to-build.zip>from Windows PowerShell v5.1 to unblock it.publishfolder containingpwsh.exe. Please run thispwsh.exethe same way as you run the v7.4.13 and share the reported error back here.segore commentedon Dec 3, 2025
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')
daxian-dbw commentedon Dec 4, 2025
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.zippackage at https://github.com/PowerShell/PowerShell/actions/runs/19913053584/job/57085927902?pr=26565#step:3:399Can you please try out the new
build.zippackage produced in CI and share the output back?set _debug_me_=1to set the environment variable_debug_me_pwsh.exeunzipped from the package in the same CMD window.segore commentedon Dec 4, 2025
PowerShell 7.6.0-preview.6
---- UpdatePath ----
path: ;
pathToAdd: C:\Users\user\OneDrive - company & name namepart2\Dokumente\PowerShell\Modules
insertIndex: 0
-------- END --------
---- UpdatePath ----
path: ;
pathToAdd: C:\Users\user\OneDrive - company & 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 ‘;’?
daxian-dbw commentedon Dec 9, 2025
Yes, the resulting
;from the encoded&caused the single path to be treated as 2 paths separated by thePath.PathSeparator, which is unexpected and led to the exception.The path
C:\Users\marcv\OneDrive - company & name namepart2\Dokumente\PowerShell\Modulesis the personal module path constructed byEnvironment.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.segore commentedon Dec 9, 2025
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.
iSazonov commentedon Dec 9, 2025
We still need to return a useful message instead of a crash.
daxian-dbw commentedon Dec 10, 2025
Yes, there are 2 issues that we need to address:
ReportExceptionFallback(Exception e, string header)does not write out stack trace of the exception, which makes it hard to diagnose failures likeShellCannotBeStarted.UpdatePathmethod used in module path construction doesn't handle the case wherepersonalModulePathToUsemay contain multiple paths separated by thePathSperator(user can definePSModulePathinpowershell.config.jsonwith such a value).The PR with fixes was submitted: #26565