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 1014281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:13:47+00:00 2026-05-16T10:13:47+00:00

I am building an automation system using Powershell and I would like to define

  • 0

I am building an automation system using Powershell and I would like to define custom .Net types so that I can structure my application in a similar way to the other code I write.

At first, I tried using ‘Add-Type’ to import types from c# files into my application but I found this to be extremely painful to debug as a .net type can only be loaded once during per Powershell session. This means that when there are errors a single change to my code requires me to restart my app (I might as well be using C#).

At the moment I am simulating types in Powershell by using factory functions that dynamically generate PSObjects with the semantics I need. This does work but it is very verbose and I don’t seem to be able to declare private variables or methods for encapsulation.

Is there an easier way to achieve what I want?

If not….

Am I somehow barking up the wrong tree by wanting to write my Powershell code using abstract data types?

Is there a better way to structure my system that I should know about?

Can I achieve some encapsulation with what I am doing now?

  • 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-05-16T10:13:48+00:00Added an answer on May 16, 2026 at 10:13 am

    1) I haven’t tried it, but this project looks promising: PSClass – Object Oriented Scripting in Powershell. Some features it offers:

    PsClass is currently implemented in
    Powershell in a single file. Makes it
    simple to use. It supports the
    following OO concepts.

    • Inheritance
    • Polymorphism
    • Encapsulation
    • Constructors with parameters
    • Notes – read-write variables
    • Methods – scriptblocks
    • Properties with Get scriptblocks and optional Set scriptblocks
    • Static and Private Notes and Methods

    2) You could use custom modules like this:

    $o = New-Module -AsCustomObject -Function GetVar, AddToVar {
        $script:var = 1
        function checkRange { if ($var -gt 3 -or $var -lt 0) { throw 'not in range <0,3>' } }
        function GetVar { $script:var }
        function AddToVar { param($add) checkRange; $script:var += $add; $script:var }
    }
    $o.GetVar()
    $o.AddToVar(2)
    $o.AddToVar(1)
    $o.GetVar()
    $o.checkRange()
    $o | gm
    

    Try to copy it to your ide/console and run it. $script:var is private member that can be used only from functions GetVar,AddToVar. checkRange is private as well.


    Personally I wouldn’t use PowerShell for such a task if I needed real OOP (available in C#). PowerShell has some support for objects, but very poor for OOP.

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

Sidebar

Related Questions

We have an application (a custom network management tool for building automation) that supports
Does anyone have experience building a .NET test execution harness that can execute the
I would like to use C# for an application I'm building, but the application
I know that you can control an MMC snap-in using the MMC 2.0 Automation
building a site using PHP and MySQL that needs to store a lot of
Building a new ASP.net application, and planning to separate DB, 'service' tier and Web/UI
I'm building a Windows Forms system (in C# if it matters to anyone) that
I'm building a small GUI-Test automation tool in C# for a application. One of
I'm building an Android app that resizes based on the screen size using different
In the system I am building user CAN NOT register them selves. The users

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.