Want to Influence the PowerShell Cookbook V2?

We’ve started working on the next edition of the PowerShell Cookbook, and one obvious goal is to improve on the first version.

As the first version has been in print, I’ve taken notes on where people get confused with certain recipes. I’ve taken notes on what I felt were content gaps, and taken the feedback from reviews on Amazon.com and random blogs. Reviews on Amazon are GOLD for authors. They help readers form educated opinions, and provide helpful feedback about the book itself. If you want to thank the author of a book you like, write a review on Amazon.

Moving and Deleting Really Locked Files in PowerShell

Once in awhile, you need to do brain surgery on files locked by the system. This is a common problem run into by patches and hotfixes, so Windows has a special mechanism that lets it move files before any process has the chance to get its grubby little hands on it. This can only be done during a reboot, leading to the dire warning given to you by many installers.

Making Perfect Change with the Fewest Coins

I’ve long wondered exactly how few coins you need in your pocket in order to perfectly round out any bill. I usually grab a handful and hope it works out. Even that mathematically astute technique sometimes leaves me a nickel or few pennies short, though, so I settle for making change that gets me a quarter back instead of yet another handful of ore.

Even this settle-for-second-best option isn’t that great. It can cause permanent damage to unsuspecting cashiers that aren’t so good at math. Wondering why you would ever give them $1.13 for a $0.88 bill, they’ll often just give you your change back AND then the stack of coins they were originally planning to load you up with.

More PowerShell Syntax Highlighting

Vladimir Averkin recently wrote a series of posts that show how to export code with syntax highlighting into HTML and RTF formats. It works great in Outlook, but was causing Windows Live Writer to crash. The reason is that the HTML stream of the clipboard isn’t just a blob of HTML – it’s supposed to be placed into the clipboard as CF_HTML. Investigation of that issue gave enough information to exactly pinpoint the crash in Live Writer, which they were quick to resolve once we pointed out. So it was a positive thing after all :)

PowerShell Script Encrypter

We frequently get questions asking, “Where can I get a PowerShell script encoder so I can write secure scripts like the Visual Basic Script Encoder?”

The answer is that it is impossible to hide the password from the user if the script ever needs it. This is true of PowerShell, VBScript, C#, C++, Assembly, or any other language. There will always be some point when your script has reversed all of the encryption / protection mechanisms, giving the “attacker” complete access to it. If you don’t want the password itself hanging around in a script file, you can prompt the user for it. If the user is never supposed to know it, then you need to re-think your architecture.

Scripting WinDbg with PowerShell

The WinDbg module lets you script and automate WinDbg sessions from PowerShell.

PowerShell P/Invoke Walkthrough

In version 1 of PowerShell, it was possible to access Win32 APIs in one of two ways: by generating a dynamic assembly on the fly (you wouldn’t really do this for one-off calls, but would probably do it in a script that makes it easier to invoke Win32 APIs,) or by looking up the P/Invoke definition for that API call, and compiling in-line C# to access it.

These are both good approaches, but we wanted to use the Add-Type cmdlet to really nail the language interop scenarios for PowerShell V2. Add-Type offers four basic modes of operation:

DateTime Casts are Language Primitives

One thing that sometimes comes as a surprise (most recently as a comment on the PowerShell blog) is that DateTime casts are always parsed in the en-US DateTime format:

Casting to a DateTime object doesn’t respect the current culture of the thread. For example:

> [Threading.Thread]::CurrentThread.CurrentCulture

LCID  Name

-—  —-

2057  en-GB

> [DateTime] “01/02/03”

02 January 2003

> # Note US date interpretation.

This is an explicit design goal.

Should I Refinance? PowerShell the Financial Advisor

When it comes to personal finance, research and reading sometimes can’t answer all of your questions. Especially when it comes to projecting the future, many subtle factors are simply too difficult to easily describe in rules of guidance. Because of this, they tend to be ignored. With interest rates sinking, now’s a good time to evaluate a mortgage refinance. The online calculators help you figure out if it’s right for you, but they all miss important facts. Let’s pretend that you refinance 5 years into your current 30-year mortgage (that started at 108k), plan to move in 5 years, and your house value doesn’t change. You’ve now got a 100k mortgage at 6%, and want to re-finance at 5%. The immediate benefit pointed out is that your monthly payment will drop, and that the new interest-heavy payment will be even more tax deductible. At 6%, you’re paying $647 per month. At 5%, you’re paying $585 per month. However,

Showing Calendars in your OOF Messages

The Outlook team has a tradition of putting calendar diagrams in their OOF messages to try and help clarify the date ranges. In a recent mail to an Outlook-heavy DL, about 50% of the OOF messages I got back contained these mini OOF calendars, so they must find them helpful.

I asked if it was a new-fangled feature in O14, but in fact they do it by hand! (I wonder if they book vacation time for hand-crafting the OOF message.)