Way back in February last year, Jeffrey Snover posted an article on the PowerShell blog describing how to support -Whatif, -Confirm and -Verbose in PowerShell functions and scripts. As he states in that article, this is very important because it shows you how you can give consumers of your function or script the same safeguards that they get in PowerShell cmdlets. If you haven’t read the article and you are sharing scripts that make system changes with others, you should take a few minutes to read it and strongly consider (read: this is really a good idea) using the Should-Process function it describes in those scripts. You can find that article here.
That said, there is one issue that I do have with the implementation of the Should-Process function as posted in that article — it is full of hard-coded English strings when it doesn’t need to be. At the time of this writing PowerShell is already localized in 10 languages and is already being used heavily internationally, so it makes sense to provide localized PowerShell scripts whenever possible, right? That might sound daunting, but since the strings used in the output generated by -Whatif, -Confirm and -Verbose are already stored in the PowerShell resource string tables, we should be able to extract them using PowerShell itself and then use the extracted, localized strings in our scripts.
So how do we go about doing this? The answer is pretty simple: use Get-PSResourceString.
Get-PSResourceString is part of the CmdletExtensionLibrary.ps1 script that I published a little while ago. It is used to retrieve localized resource strings from the PowerShell engine and host. Using that function I was able to convert all of the strings that are used in Jeffrey’s version of the Should-Process function into calls to Get-PSResourceString so that they will be localized. The end result is a localized1 version of the Should-Process function that I have also included in the most recent version of the CmdletExtensionLibrary.ps1 script.
Both of these functions are quite long and not very blog-friendly, so I’ll leave it up to you to open the CmdletExtensionLibrary.ps1 script file in the editor of your choice and take a look at them. This script originally started as a small project to dig into dynamic parameters, but I’ve found the functions I created in that work so useful myself that I’ve been adding more and more to it, a trend that will likely continue. If you only want the Get-PSResourceString and Should-Process functions, you can simply copy them out of that script and use them however you like. And of course, if you see any issues with the functions in this script, have any questions, or want to share suggestions on how to improve it even more, I’m more than happy to listen.
1 Note that while I’m trying to provide localized scripts as well as functions to facilitate writing localized scripts to support the international PowerShell community, all documentation for these functions is only in English and there are a few hard-coded strings only in English for variable and alias descriptions and one output message in English for the script file itself. Aside from those few exceptions (that you’re not likely to see anyway), if you’re using a localized version of PowerShell the functions in the CmdletExtensionLibrary.ps1 file should properly support your localized version.