Wheres the command history in PowerShell ISE stored at?
Asked Answered
W

1

11

It seems like the command history of PowerShell is stored separately from PowerShell ISE. But I have one question is that where is the commands history of PowerShell ISE stored at and how do I view them?

Wallraff answered 18/5, 2021 at 15:27 Comment(7)
PowerShell ISE doesn't have command history, so the answer is "nowhere" :-)Hilaryhilbert
You can use Get-History in the session your on to view the command history. I believe it's stored in memory for just that session, so when it's gone, it's gone. Lol you can increase the buffer size tho in properties for your ise, or using $MaximumHistoryCount = 1000Helminth
I stand corrected, you can find it using: (Get-PSReadlineOption).HistorySavePath. Its saved under your appdata folder. So use Cat (Get-PSReadlineOption).HistorySavePath to get all historical commands ran.Helminth
@AbrahamZinala Have you tested this? Am 99% PSReadLine does not support saved history in ISEHilaryhilbert
Hmmm, only Get-History worked but, PSReadline didn't. Good catch, was trying it on a regular posh console.Helminth
As noted, the ISE is a GUI app that does not use the PSReadline module (which only works in a console, not GUI). It is the PSReadline module that saves persistent history to disk, which doesn't work in the ISE - therefore there is no persistent (i.e., saved to disk) history for the ISE.Thralldom
If you want to keep and have access to history in the ISE, then put Start-Transcript in your profile or all user profile, or set it via Local Policy, and then it will capture what you do in each PowerShell session, that you can later review as needed. Understand that this is not like PSReadline. To repeat a command, you have to read that file to pull it. Each time you open and close an ISE or PowerShell session, it will create a new timestamped file. I've been doing it this way for years because I live in the ISE 95% all day and I want a record of all I did.Pyxidium
P
18

As for the location of history files; they are located here:

Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine" | 
Format-Table -AutoSize
# Results
<#
    Directory: C:\Users\YourUserName\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine


Mode          LastWriteTime Length Name                                   
----          ------------- ------ ----                                   
-a----  17-May-21     02:23 258925 ConsoleHost_history.txt                
-a----  11-May-21     01:20 120222 Visual Studio Code Host_history.txt    
-a----  27-Jun-20     18:58      0 Windows PowerShell ISE Host_history.txt
#>

As noted, in the ISE, you can just leverage the Start-Transcript cmdlet to capture all you do, but again, it is not an out-of-box/console/ISE immediate lookup of previous commands to execute like PSReadLine. Yet, you can do that, sort of, with some creativity. Yet, that's just a kludge.

A note about using Start-Transcript. It will default to your "$env:USERPROFILE\Documents" folder.

enter image description here

So, I'd recommend you set to go to a specific folder. Also, though the files can be small, there will be tons of them over time, thus, you need to manage that.

They of course can just be opened in the ISE directly:

psEdit -filenames ((Get-ChildItem -Path "$env:USERPROFILE\Documents" | Sort-Object -Property LastWriteTime -Descending)[0]).FullName

Yet, since you are already in the ISE, you can just type and run all commands from the editor pane, and as needed, select anyone and just run it again.

Yet if you are just using the ISE console, and thinking it is the same as the PowerShell consolehost, then that's wrong. It's really an output window with some console skills. Back in the early ISE days, there were 3 panes. Editor, output window and true console.

Legacy PowerShell ISE 3 pane editor

The true console got removed in later ISE versions. Why, who knows? VSCode almost got us back there.

If you want to do the console stuff, then use the PowerShell console, or shell to the PowerShell console using runspaces to stay in the ISE.

For the runspaces thing, here is an example to run PowerShell Core (v6+), while still in the ISE.

Using PowerShell 7 in the Windows PowerShell ISE

https://old.ironmansoftware.com/using-powershell-core-6-and-7-in-the-windows-powershell-ise

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to PowerShell 7", { 
        function New-OutOfProcRunspace {
            param($ProcessId)

            $ci = New-Object -TypeName System.Management.Automation.Runspaces.NamedPipeConnectionInfo -ArgumentList @($ProcessId)
            $tt = [System.Management.Automation.Runspaces.TypeTable]::LoadDefaultTypeFiles()

            $Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($ci, $Host, $tt)

            $Runspace.Open()
            $Runspace
        }

        $PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
        $Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
        $Host.PushRunspace($Runspace)
}, "ALT+F5") | Out-Null

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", { 
    $Host.PopRunspace()

    $Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
    $Child | ForEach-Object { Stop-Process -Id $_.ProcessId }

}, "ALT+F6") | Out-Null

So, with a bit of tweaking, you can do the same for Windows PowerShell 5 to get the raw console

But again,m avoid all the hoops, and use VSCode, if you are allowed. Yet on your servers, we all know ISE is still a thing. ;-}

Pyxidium answered 18/5, 2021 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.