Ever wish you had history like in unix with .bash_history?
Here is some code that you can put in your profile file that defines a function BYE which saves off your sessions history into a History.CSV file in your home directory and then adds that to your history when you startup the next session. You get to control how much of the history you want (up to 32KB-1) but I just do 1KBs worth.
I was going to put comments in the code but thought I would test out my assertion that VERBOSITY produces self-documenting scripts. I think it does but you can let me know one way or the other.
So add this to your startup and then do a session, type BYE instead of EXIT and then start a new session and do a Get-History. You'll see a bunch of commands already in your list.
Now lets have a little fun with History, do what I instructed above and get into your new session. Now do a Get-History and pipe it to Get-Member:
The script you've shared is quite handy for maintaining command history across PowerShell sessions. It essentially creates a function called bye that saves the session history to a CSV file upon exit and reloads it when the session starts again. This method can be effective for tracking commands and activities across sessions. Here's a breakdown of the script and the additional instructions you provided:
Script Explanation:
$MaximumHistoryCount = 1KB: Sets the maximum history count to 1KB.
New-Item ~\PowerShell -ItemType Directory: Creates a PowerShell directory if it doesn't already exist.
The script you've shared is quite handy for maintaining command history across PowerShell sessions. It essentially creates a function called bye that saves the session history to a CSV file upon exit and reloads it when the session starts again. This method can be effective for tracking commands and activities across sessions. Here's a breakdown of the script and the additional instructions you provided:
Script Explanation:
$MaximumHistoryCount = 1KB: Sets the maximum history count to 1KB.
New-Item ~\PowerShell -ItemType Directory: Creates a PowerShell directory if it doesn't already exist.
bye Function:
Get-History -Count 1KB | Export-CSV ~\PowerShell\history.csv: Saves the last 1KB worth of history to a CSV file upon exiting.
exit: Exits the PowerShell session.
Import-CSV ~\PowerShell\History.csv | Add-History: Loads the saved history back into the session if the history file exists.
Fun with History:
Verify saved history by executing Get-History in a new session after using bye to exit the previous session.
Get-History | Get-Member -MemberType Property: Displays properties of Microsoft.PowerShell.Commands.HistoryInfo.
Example Command to Determine Execution Time:
ghy | ft id, endexecutiontime, commandline -auto: Displays the ID, end execution time, and command line of each history entry. Using these functions can indeed make your PowerShell session history more dynamic and informative. Additionally, checking the properties and execution times of commands provides more context to your command history. If you have any specific questions or need further assistance with PowerShell or scripting, feel free to ask!