In PowerShell, you can create real objects with behavior (methods) using the Add-Member cmdlet. Here's an example code that demonstrates how to do this:
$person = New-Object -TypeName PSObject $person | Add-Member -MemberType ScriptMethod -Name Greet -Value { param([string]$name) "Hello, $name!" } $person.Greet("John") In this example, a new object is created using the New-Object cmdlet and stored in the $person variable. The Add-Member cmdlet is then used to add a script method named Greet to the object. The Greet method takes a string parameter $name and returns a string that greets the specified name.
To call the Greet method, simply call the method on the $person object and pass in a name parameter. In this example, the method is called with the name "John", which produces the output "Hello, John!".
Note that you can add multiple script methods to an object using the Add-Member cmdlet, and you can also add other types of members, such as properties and events. This allows you to create fully-functional objects with behavior in PowerShell.
"PowerShell class with a simple method"
class MyClass { [string] MyMethod() { return "Hello from MyMethod!" } } $myObject = [MyClass]::new() $result = $myObject.MyMethod() MyClass with a simple method MyMethod. Creates an instance of the class and calls the method."PowerShell class with method parameters"
class Calculator { [int] Add([int]$a, [int]$b) { return $a + $b } } $calculator = [Calculator]::new() $result = $calculator.Add(5, 3) Calculator with a method Add that takes parameters. Creates an instance and calls the method with arguments."PowerShell class with constructor and methods"
class Person { [string] $Name Person([string]$name) { $this.Name = $name } [string] Greet() { return "Hello, $($this.Name)!" } } $person = [Person]::new("John") $greeting = $person.Greet() Person with a constructor and a method Greet. Creates an instance by providing constructor arguments."PowerShell class with private method"
class Example { [string] PrivateMethod() { return "This is a private method." } [string] PublicMethod() { return $this.PrivateMethod() } } $instance = [Example]::new() $result = $instance.PublicMethod() PrivateMethod is called from a public method."PowerShell class with static method"
class MathOperations { static [int] Add([int]$a, [int]$b) { return $a + $b } } $result = [MathOperations]::Add(7, 3) MathOperations with a static method Add. Calls the static method without creating an instance."PowerShell class inheritance"
class Animal { [string] $Sound Animal([string]$sound) { $this.Sound = $sound } [string] MakeSound() { return "The animal makes $($this.Sound) sound." } } class Dog : Animal { [string] Bark() { return "Woof! Woof!" } } $dog = [Dog]::new("barking") $dogSound = $dog.MakeSound() $bark = $dog.Bark() Animal and a derived class Dog. The Dog class inherits from Animal."PowerShell class with method overloading"
class Overloader { [string] DoSomething([string]$param) { return "String: $param" } [int] DoSomething([int]$param) { return $param * 2 } } $instance = [Overloader]::new() $resultString = $instance.DoSomething("Hello") $resultInt = $instance.DoSomething(5) Overloader having two methods with the same name but different parameter types."PowerShell class with method validation"
class Validator { [int] ValidateNumber([int]$number) { if ($number -gt 0) { return $number } else { throw "Number must be greater than 0." } } } $validator = [Validator]::new() $validNumber = $validator.ValidateNumber(10) ValidateNumber method checks if the provided number is greater than 0."PowerShell class with method returning custom objects"
class Book { [string] $Title [string] $Author Book([string]$title, [string]$author) { $this.Title = $title $this.Author = $author } } class Library { [Book] GetBook() { return [Book]::new("PowerShell 101", "John Doe") } } $library = [Library]::new() $book = $library.GetBook() Library with a method GetBook that returns an instance of another class Book."PowerShell class with method chaining"
class Chainer { [string] $value Chainer([string]$initialValue) { $this.value = $initialValue } [Chainer] Append([string]$appendValue) { $this.value += $appendValue return $this } } $result = [Chainer]::new("Hello").Append(" World").Append("!") Chainer). The Append method appends a value to the existing value and returns the updated object.blurry python kaggle stargazer delphi-xe8 environment-variables android-scrollview sidekiq catplot aws-amplify