Deprecation of cmdlets
Until PowerShell 3.0, if you shipped a cmdlet there was no good way to deprecate it. Cmdlet developers can use the .NET ObsoleteAttribute now to let users know that they should not use a cmdlet anymore. In the following example, the cmdlet can run but it will issue a warning that another cmdlet should be used from now on. This gives the user time to prepare for the change and fix any scripts dependent on old commands.
$code = @'
using System;
using System.Management.Automation;
namespace ObsoleteCmdlet
{
[Cmdlet("Get","Foo")]
[Obsolete("This cmdlet is obsolete. Please use Get-Bar instead.")]
public class ObsoleteCmdlet : PSCmdlet
{
protected override void ProcessRecord()
{
WriteObject("Hello");
}
}
}
'@
#compile this code into the ObsoleteCmdlet.dll
PS> Add-Type -TypeDefinition $code -OutputAssembly $env:TEMP\ObsoleteCmdlet.dll
# Import the module and run our cmdlet
PS> Import-Module $env:TEMP\ObsoleteCmdlet.dll
PS> Get-Foo
WARNING: This cmdlet is obsolete. Please use Get-Bar instead.
Hello
When you ship the next version of your cmdlet you can change the behavior of the Obsolete attribute, by using another constructor, and throw an error instead of a warning:
# [Obsolete("This cmdlet is obsolete. Please use Get-Bar instead.",true)]
PS> Get-Foo
This cmdlet is obsolete. Please use Get-Bar instead.
At line:1 char:1
+ get-foo
+ ~~~~~~~~
+ CategoryInfo : InvalidOperation: (Get-Foo:String) [], RuntimeException
+ FullyQualifiedErrorId : UseOfDeprecatedCmdlet
It would be great to have this attribute in advanced functions and even on parameters. I logged a suggestion on connect about this, vote it up if you like it.