Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make readonly members in powershell?

How do I make members readonly when I use Add-Member cmdlet in Powershell?

Basically, I want to add-member to a System.Diagnostic.Process which has a readonly property.

like image 791
Bill Avatar asked Aug 09 '11 00:08

Bill


People also ask

How do I change permissions to read only in PowerShell?

To do this, we use the Set-ItemProperty command with the property name of IsReadOnly and set the Value to $true. Doing so produces no output, but we can confirm it's set to read-only now by using Get-ItemProperty and checking on the IsReadOnly property again. Now you can see the IsReadOnly property is set to True.

Is a readonly property PowerShell?

Although Powershell doesn't have real readonly class properties, we can mimic them in two elegant ways: Class methods as getter and setter functions. Script properties with getter and setter functions.

How do I get file attributes in PowerShell?

To get file attributes in PowerShell, you can use Get-ChildItem or Get-Item cmdlets. It returns the file attributes or properties available on the specified files. To get the list of all properties available, use the Get-Member cmdlet.


1 Answers

Like so:

 $p = new-object System.Diagnostics.Process
 $p | Add-member -Name thisisreadonly -membertype scriptproperty -value { 6}
 $p.thisisreadonly #gives 6
 $p.thisisreadonly = 5 #error- Set accessor for property "thisisreadonly" is unavailable.

So basically you create a ScriptProperty, with a getter but no setter.

like image 139
manojlds Avatar answered Sep 26 '22 17:09

manojlds