A bacon- (and module-) saving PowerShell incident
February 1st, 201112
A bacon- (and module-) saving PowerShell incident
February 1st, 201112

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.

By: Aaron Bertrand

I am a passionate technologist with industry experience dating back to Classic ASP and SQL Server 6.5. I am a long-time Microsoft MVP, write at Simple Talk, SQLPerformance, and MSSQLTips, and have had the honor of speaking at more conferences than I can remember. In non-tech life, I am a husband, a father of two, a huge hockey and football fan, and my pronouns are he/him.

12 Responses

  1. Richard says:

    Or "Previous Versions" if you're on Vista or 7.

  2. David says:

    Good save.  But at least you could have dug it out of the Backups … you do have backups, right ?? 🙂

  3. AaronBertrand says:

    Yep. Like I said, out of the Notebook habit for several years. I learned a lot more from the save than from my bad practices.

  4. Kenny says:

    Aaron – I'd suggest a habit of at least making a copy of the file first *before* opening it (i.e. the copy) in Notepad if you're going to save your changes as another copy anyways. Nice save though!

  5. AaronBertrand says:

    Still not sure how that helps. Does source control have some kind of quality check? No. I can write a script with an infinite loop and it will still do that whether or not I checked it into source control first. I'm not against source control, and I use it where appropriate. I just don't agree that you need to use it for every single line of code you write in your entire career.

  6. Tyler says:

    Never the less, a source control system is always better than executing things directly on a customer's server… We try to do that only in real emergencies… 🙂

  7. AaronBertrand says:

    When I'm writing one-off and single-use PowerShell scripts on a client's system? No, I think source control would be overkill in a lot of these scenarios.

  8. Senol Balaban says:

    I'm not a scripter but, wouldn't be usefull and much better to use a version control sytem(e.g. SVN)?

  9. Jeffery Hicks says:

    I wrote a function for exporting functions (sounds chaotic I know) because you never know.  This is a great example.
    That's also why you should never use Notepad. 😉  At least use the ISE which does have some Undo functionality.

  10. Rhys says:

    nice, one for the bookmarks!

  11. Peter Schott says:

    Good info, but I'd likely try to see if I could run something like http://portableapps.com/apps/development/notepadpp_portable. I realize that's not always a possibility either, especially in cases like yours.

  12. Joe Webb says:

    Thanks, Aaron! Great info.  I hope I never need this, but you never know.