Open
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues, especially the pinned issues.
Exception report
Exception calling "GetHistoryItems" with "0" argument(s): "Object reference not set to an instance of an object."
At C:\Users\Sergey.Simonchik\sandbox\init.ps1:3 char:3
+ $HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException
Screenshot
Environment data
PS Version: 7.4.1
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.3.4
PSReadLine EditMode: Windows
OS: 10.0.22621.1 (WinBuild.160101.0800)
BufferWidth: 163
BufferHeight: 45
Steps to reproduce
I would like to run a small init script before a PowerShell session starts to access PSReadLine command history there. Unfortunately, [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()
fails with the error.
pwsh.exe -NoExit -ExecutionPolicy Bypass -File .\init.ps1
#init.ps1
if (Get-Module -Name PSReadLine) {
$HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()
if ($HistoryItems -eq $null) {
[Console]::WriteLine("got null")
}
else {
[Console]::WriteLine("got not-null")
}
}
Expected behavior
History items can be accessed successfully, no exception happens and not null
is printed to the console.
Actual behavior
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS C:\Users\Sergey.Simonchik> cd .\sandbox\
PS C:\Users\Sergey.Simonchik\sandbox> pwsh.exe -NoExit -ExecutionPolicy Bypass -File .\init.ps1
PowerShell 7.4.1
MethodInvocationException: C:\Users\Sergey.Simonchik\sandbox\init.ps1:3
Line |
3 | $HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryI …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "GetHistoryItems" with "0" argument(s): "Object reference not set to an instance of an object."
got null
PS C:\Users\Sergey.Simonchik\sandbox>