Skip to content

Instantly share code, notes, and snippets.

@OCram85
Created December 6, 2016 07:55
Show Gist options
  • Save OCram85/03ce8c0f881477c835e3fdfc279dfed7 to your computer and use it in GitHub Desktop.
Save OCram85/03ce8c0f881477c835e3fdfc279dfed7 to your computer and use it in GitHub Desktop.
Creating Getter/Setter method within powershell class
Class FooBar {
[string]$Prop1
Foobar() {
$this | Add-Member -Name Prop1 -MemberType ScriptProperty -Value {
# This is the getter
return $this.Prop1
} -SecondValue {
param($value)
# This is the setter
$this.Prop1 = $value
} -Force
}
}
$FooBar = [Foobar]::new()
@OCram85
Copy link
Author

OCram85 commented Dec 6, 2016

This breaks if you try to access the property with someting like $FooBar.Prop1 or $FooBar.Prop1 = "This doesn't work..."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment