You want:
Accessor methods like get_<PropertyName>()
to be hidden automatically when a class instance is created.
If the accessor method is explicitly declared with the hidden
keyword, the related ScriptProperty
should also be hidden.
To be able to toggle this "hidden" visibility programmatically at runtime (not just at design-time with hidden
).
In short: Can you dynamically hide methods (e.g., from Get-Member
) after class creation in PowerShell?
PowerShell classes are just thin wrappers over .NET types. Once the type is emitted, the metadata about its members (visibility, attributes, etc.) is fixed. Unlike C#, PowerShell does not expose any supported mechanism to rewrite or patch that metadata at runtime.
Get-Member enumerates members from the object’s type metadata (methods, properties, etc.) plus any extended members in the PSObject layer.
Class methods/properties are baked into the type when PowerShell compiles the class
. They are not dynamic.
The hidden
keyword is a compile-time modifier that marks members with [System.Management.Automation.HiddenAttribute]
. This is checked by Get-Member
.
Attributes in .NET are immutable once constructed. Even though your C# POC tries to mutate [Browsable]
, this is not a general solution in PowerShell — those attributes aren’t consulted by Get-Member
.
Since you can’t change class methods at runtime, here are workarounds:
Add-Member
/ PSObject
layer instead of class
If you attach script properties/methods dynamically:
$o = [PSCustomObject]@{}
$o | Add-Member -MemberType ScriptMethod -Name "MyMethod" -Value { "Hello" }
# Hide it later
$o.PSObject.Members["MyMethod"].IsHidden = $true
Now Get-Member
won’t show MyMethod
, because IsHidden
works on PSObject members.
This gives you the runtime flexibility you’re asking for, but not within class
.
hidden
at design timeIf you’re sticking with classes, the only supported way:
class MyClass {
hidden [string] HiddenMethod() { "secret" }
}
This hides it from Get-Member
, but you cannot toggle later.
You can keep your logic in a class, but expose accessors as PSObject script properties, which you can hide/unhide dynamically:
class MyClass {
[string] Get_Secret() { "hidden stuff" }
}
$inst = [MyClass]::new()
$ps = [PSCustomObject]@{ Base = $inst }
$ps | Add-Member ScriptProperty Secret { $this.Base.Get_Secret() }
$ps.PSObject.Members["Secret"].IsHidden = $true
Now you have a class internally, but only expose dynamic script properties externally, where you can control visibility.
Classes are static: once compiled, their member visibility cannot be changed.
Dynamic objects (PSCustomObject + Add-Member) are the right tool if you want runtime mutability.
Get-Member doesn’t consult [Browsable]
; the only attribute it respects is [Hidden]
.
Is it possible to hide a class method programmatically at runtime?
No. PowerShell classes are static, and hidden
must be used at design time.
What’s the alternative?
Use PSObject
+ Add-Member
for dynamic script properties/methods, which support toggling IsHidden
.
Impact on Get-Member
:
Class methods always appear unless marked hidden
at compile time. For true runtime control, wrap with a dynamic object.
If you want, I can draft a POC “Use-ClassAccessors” helper that builds instances using PSObject
+ Add-Member
, so you can keep the same API style but gain runtime hide/unhide capability.
Would you like me to sketch that?