Protecting Functions

by Nov 22, 2012

To prevent a function to be redefined or overwritten, you can write-protect it:

function Test-Function { 'Hello World!' }
Set-Item -Path function:Test-function -Options ReadOnly

From this point, your function cannot be overwritten. However, it can be deleted and then redefined. So this is the same level of protection you can expect from the readonly attribute in the file system. It protects you from accidental changes. It won't protect you from intentional changes.

Twitter This Tip! ReTweet this Tip!