Earlier today I made a big goof. I opened a module in Notepad, intending to use it as the basis for a new module. I was in the process of using "File > Save As" when my phone rang just at the precise instant that, for some reason, made me click on "File > Save" by mistake. After hitting Ctrl+Z 30 times to try to get the old version of the module back, I remembered that Notepad has never had more than one level of Undo. Back when I was coding ASP by hand, I was very well aware of this, but I hadn't come across this situation in a long time, so I was a bit rusty.
I went back and forth with Aaron Nelson a bit on how to recover from this grave error, and I came across this gem, which I could run, thankfully, because I had several windows with the module already loaded:
$f = "FunctionName"; (Get-Command $f Show-List).Definition;
That let me see the original function on the screen, but I wanted it in a file so I wouldn't have to transcribe it. Easy enough:
$f = "FunctionName"; (Get-Command $f Show-List).Definition | Out-File -Append "C:\Aaron\Moron.txt";
If I had messed up more than one function in the module, I would have had to repeat this process multiple times; in this case, I had only touched a single function.
Just wanted to point out that little tip in case any of you ever find yourself in the same situation. (And, admittedly, so that I can easily find the command should I ever do this to myself again.)
Yes, I hear all of you screaming at me because I'm an idiot and I shouldn't be using Notepad. Yep, you're probably right. Just remember that we're not always working on our cozy little personal machines with all of our preferred tools and applications set up – sometimes we have to work when RDP'd into a server that we've never used before, and it is not always prudent to make it feel like home because, well, it's not.