Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell history: how do you prevent duplicate commands?

Tags:

Background:

PowerShell history is a lot more useful to me now that I have a way to save history across sessions.

# Run this every time right before you exit PowerShell
get-history -Count $MaximumHistoryCount | export-clixml $IniFileCmdHistory;

Now, I am trying to prevent PowerShell from saving duplicate commands to my history.

I tried using Get-Unique, but that doesn't work since every command in the history is "unique", because each one has a different ID number.

like image 531
dreftymac Avatar asked Sep 17 '09 13:09

dreftymac


People also ask

Does PowerShell Save command history?

PowerShell automatically maintains a history of each session. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable. Beginning in Windows PowerShell 3.0, the default value is 4096 .


2 Answers

Get-Unique also requires a sorted list and I assume you probably want to preserve execution order. Try this instead

Get-History -Count 32767 | Group CommandLine | Foreach {$_.Group[0]} |
Export-Clixml "$home\pshist.xml"

This approach uses the Group-Object cmdlet to create unique buckets of commands and then the Foreach-Object block just grabs the first item in each bucket.

BTW if you want all commands saved to a history file I would use the limit value - 32767 - unless that is what you set $MaximumHistoryCount to.

BTW if you want to automatically save this on exit you can do this on 2.0 like so

Register-EngineEvent PowerShell.Exiting {
  Get-History -Count 32767 | Group CommandLine |
  Foreach {$_.Group[0]} | Export-CliXml "$home\pshist.xml" } -SupportEvent

Then to restore upon load all you need is

Import-CliXml "$home\pshist.xml" | Add-History
like image 74
Keith Hill Avatar answered Sep 29 '22 15:09

Keith Hill


The following command works for PowerShell in Windows 10 (tested in v.1803). The option is documented here.

Set-PSReadLineOption –HistoryNoDuplicates:$True

In practice, calling PowerShell with the following command (e.g. saved in a shortcut) opens PowerShell with a history without duplicates

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command Set-PSReadLineOption –HistoryNoDuplicates:$True
like image 35
divenex Avatar answered Sep 29 '22 16:09

divenex