This post is less on doing something useful and more on just proving that I can do something even though PowerShell is trying to tell me that I cannot do it. That little thing is updating a property on an object that is set as Read-Only.
My example to show you how you can do something like this is by looking at $PSVersionTable which as we all know gives us some great information about what version of PowerShell that we are using.
I am also using this as this was an example used in our previous user group meeting in which we were shown that the properties here could not be updated.
From this object, I am going to look more at the PSVersion property which of course is an object within this object.
Looking at the properties of this object, we can see that each of the properties are shown as Read-Only by only having the Get method available to use.
$PSVersionTable.PSVersion | Get-Member
Furthermore, I am greeted with a nice error stating that my attempts to modify this have been foolish and that I should just give up.
$PSVersionTable.PSVersion.Major = 10
Yea, I guess I could just throw in the towel and move onto something more useful like updating some bugs on a module, but I think that I can do better here! And by using some reflection magic, I can make this happen!
To do this, I need to look at the hidden fields for the PSVersion object.
Here we can see that the field that we need to look at updating is _Major. Now we need to figure out how we can set this.
$Field = $PSVersionTable.PSVersion.GetType().GetField('_Major','static,nonpublic,instance') $Field | Get-Member
Looking at the methods, I see a SetValue that accepts a object and a value for the object. This seems pretty simple,right? Just call the method and update the value.
Perfect! We have now updated this “Read-Only” property to give us PowerShell V10! Now this will go away once we close and re-open PowerShell but it still a fun thing to do to show off how you can update these kinds of values.