Just Code‎ > ‎

Powershell - How to Override ToString, or any method on an object

posted Nov 16, 2011, 5:43 AM by Peter Henell   [ updated Jan 25, 2012, 5:19 AM ]
You need to specify the -Force parameter to override a method on the class, otherwise you get an error saying that the method already exists. 
If you override it several times, then it will be the last supplied implementation that will be used.
$abba = [System.DateTime]::Now
$abba = $abba | Add-Member ScriptMethod ToString { $this.Day } -PassThru -Force


$abba = $abba | Add-Member ScriptMethod ToString { $this.Month } -PassThru -Force