Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:34:38+00:00 2026-06-18T15:34:38+00:00

Probably this question has been answered before…. but I have not found a specific

  • 0

Probably this question has been answered before…. but I have not found a specific answer to my needs.

BTW I’m using PowerShell 3

Well, I’m new in PowerShell but I have a lot of experience as a C# developer, so working with objects is very important to me.

So I’m wondering if there’s a clean way to apply OOP concepts (well not all of them, though that would be awesome) in a PowerShell script, for example there are specific things that I would like to do.

Note: I’m aware that I can write C# code in PowerShell to create DTO’s and also that I can create PowerShell binary modules in C#, I have done that in the past, but what I’m looking for now is the ability to write all my code in PowerShell but in an Object Oriented way.

Things I would like to do:

  • Create an object in PowerShell, and expose a function written in PowerShell as well, something like this:

    function New-Person
    (
        [Parameter()][string]$firstName
    )
    {
        function Walk()
        {
            Write-Host "Walking...";
        }
    
        $person = New-Object psobject;
    
        $person | Add-Member -MemberType NoteProperty -Name FirstName -Value $firstName;
    
        #This line does not work
        #$person | Add-Member -MemberType CodeMethod -Name Walk -Value Walk;
    
        return $person;
    }
    
    $MyPerson = New-Person -firstName "JP";
    
    $MyPerson;
    
    #This line does not work
    $MyPerson.Walk();
    
  • Encapsulate behavior, which means create functions inside my object and then mark them as private

  • [Nice to have]. Create base classes so I can inherit and specialize my behavior overriding methods

  • [Nice to have]. Create interfaces so I can start thinking in unit testing in isolation my PowerShell methods (I know there are tools like Pester to do this I’m just focusing on OOP features)

What I have done so far is creating objects with properties only (DTO’s) but I’d like to add behavior to my objects

I’d appreciate if you guys point me in the right direction

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:34:40+00:00Added an answer on June 18, 2026 at 3:34 pm

    Two options to create object with methods:

    1. Add-Member
    2. New-Module -AsCustomObject

    Code samples:

    $person | Add-Member -MemberType ScriptMethod -Value {
        'I do stuff!'
    }
    
    $person = New-Module -AsCustomObject -ScriptBlock {
        $Property = 'value'
        [string]$Other = 'Can be strongly typed'
    
        function MyMethod {
            'I do stuff!'
        }
    
    }
    

    EDIT: speaking of private/ public… In latter example property won’t show up “by default”. You can decide what is public using Export-ModuleMember and specify -Variable (properties) and/or -Function (methods) that will be public. Without explicit Export-ModuleMember it will behave same as in “normal” module – export only functions (methods).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question has probably been asked numerous times, but I have not
I think probably this question has been asked/answered several times before my post. But
This question has probably been asked before, but I am not able to find
This question has probably been answered hundreds of times, but here goes. I have
I'm sure this question has probably been answered before, so I apologize, but I
this question has probably been asked before, but i'm stuck and i've tried a
This question has probably been asked before, but I was wondering if there was
Probably this question has been answered before, if that's the case I would appreciate
I know this has probably been answered before, but I cannot find much information
This question has probably already been answered somewhere but I'm having a hard time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.