Typing ‘exit’ in a Powershell console, as you would imagine, exits the console. I use PowerShellPlus almost exclusively (and love it!). Typing ‘exit’ while in PowerShellPlus (known affectionately as simply “teh+”) gives you the option of closing the app or starting a new, clean console session. I often use this as a quick way to clean out my console environment.
I have also written a small function, named ‘exitt’, that exits AFTER securing the console. I sign all of my scripts. But sometimes, while working on something, I will set my Execution Policy to ‘remotesigned’ for a while. But, I pretty much always set my Execution Policy to ‘allsigned’ before exiting. That way, nothing unsigned by myself will accidentally (or maliciously… am I paranoid?) run the next time I start Powershell.
Anyway, back to my point…
When I type ‘exit’ or run my ‘exitt’ function and stay in teh+, my history is wiped out. This usually isn’t a problem. But, there are times when I want a clean, fresh console AND my history. To that end, I tweaked my ‘exitt’ function and my profile a bit.
(NOTE: I did this before really researching things on the Internet. There are probably better ways of handling this, but this works for me).
The meat of my ‘exitt’ function looks like this:
- param
- (
- [Parameter(Position=0, Mandatory=$false, ValueFromPipeLine=$false)]
- [switch]$history = $false
- )
- # Call Secure-Console function to set executionPolicy to AllSigned
- Secure-Console
- # If switched, export history for future use. Otherwise, blow out history
- if ($history)
- {
- Get-History | Export-Clixml "c:\scripts\hist.xml" -Force
- }
- else
- {
- Remove-Item "c:\scripts\hist.xml" -Force -ea SilentlyContinue
- }
- #Close Program
- exit
So, if I run the function with the –history switch, it writes the current history out to an XML file.
Then, my profile has this bit:
- if (Test-Path "c:\scripts\hist.xml")
- {
- Add-History (Import-Clixml "c:\scripts\hist.xml")
- Remove-Item "c:\scripts\hist.xml" -Force -ea SilentlyContinue
- }
Pretty simple, and it works for me.
After writing this, I did a quick search in the ‘tubes and came across JSnover’s solution to this. Maybe I will do that first next time.
:-)

0 comments:
Post a Comment