Creating "Constant" Functions

by Dec 23, 2009

When you make a function read-only, it can no longer be overwritten but you would still be able to delete the function and recreate it from scratch. You can make them constant if you'd like to create functions that cannot be changed as long as the PowerShell session runs. Here is how:

New-Item -Path function: -Name ImportantFunction `
-options "Constant" -Value `
{ "You cannot get rid of me except by closing powershell…!" }

If you place this in one of your profile scripts, the function will be created on each launch, and a user cannot change the function in any way.

Twitter This Tip! ReTweet this Tip!