# PowerShell classes in a module

**URL:** <https://forums.powershell.org/t/powershell-classes-in-a-module/24513>\
**Category:** PowerShell Help\
**Created:** [July 30, 2024, 7:48pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513 "2024-07-30T19:48:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [July 30, 2024, 7:48pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/1 "2024-07-30T19:48:29Z")

</div>

I am starting to use classes to allow for strong typing and consistency in object parameter types.  
I’m adding them to an existing PowerShell module containing multiple functions and psm1 files. Does anyone have a good example of how to export classes in the module so they can be more easily used in the PS session when the module is imported (i.e. for IntelliSense) or across functions in different psm1 files?

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 30, 2024, 8:06pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/2 "2024-07-30T20:06:13Z")

</div>

Others with more experience can probably chime in but my understanding is that Import-Module does not make the class definitions available in the session.  
[This blog post](https://stephanevg.github.io/powershell/class/module/DATA-How-To-Write-powershell-Modules-with-classes/) has some good examples of how Using module vs Import-Module pertains to class use.  
It’s going to feel like double the work but the approach I adopted was mirrors what is discussed in that blog post:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/f/f89a4ec691a89e34b45f7b1df832c4356fe42b13.png)  
Since not all classes necessarily need to be exposed to the user I’ll write a public function used to instantiate the object.

I haven’t done this a lot but here’s an example:  
Class definition

```auto
Class CipherObject {
    [String] $Encryption
    [String] $CipherText
    hidden[String] $DPAPIIdentity
    CipherObject ([String]$Encryption, [String]$CipherText) {
        $this.Encryption = $Encryption
        $this.CipherText = $CipherText
        $this.DPAPIIdentity = $null
    }
}

```

function definition

```auto
Function New-CipherObject {
    [cmdletbinding()]
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateSet("DPAPI","AES")]
        [String]$Encryption,
        [Parameter(Mandatory = $true, Position = 1)]
        [String]$CipherText
    )
 
    [CipherObject]::New($Encryption,$CipherText)
    
}

```

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [July 30, 2024, 9:06pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/3 "2024-07-30T21:06:03Z")

</div>

That blog post is very enlightening. So I need a function for each class I want to use, and that is used to create objects across the module (and in scripts that use the module).

But the thing I’m running into is that other functions in my module don’t recognize the class. For example…

```auto
one.psm1:
Function get-object {
    create an object of the class 'blah'
    populates the properties
}
class blah {}

two.psm1
Function use-object {param([blah]$inputobject)}

get-object | use-object -> InvalidOperation: Unable to find type [blah]

```

This is what I feel like should work but doesn’t. The scope of the class definition seems to be confined to the psm1 file and not the entire module.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 30, 2024, 9:10pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/4 "2024-07-30T21:10:03Z")

</div>

i didn’t know you could have multiple psm1 files in a module. I thought it was a psm1, and/or a psd1 file.  
I believe the class definitions **are** contained within the scope of a single psm1, but typically that means it’s the scope of a module.

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [July 30, 2024, 9:16pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/5 "2024-07-30T21:16:12Z")

</div>

You sure can - you have to list the files in the module manifest under **NestedModules** , but we have dozens of psm1 files in our team module.

Bummer then for shared class defnitions ☹

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 31, 2024, 12:21am UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/6 "2024-07-31T00:21:02Z")

</div>

it sounds hacky, but I wonder if in your module directory you could store all your class definition files in a folder called “Classes” and then in each psm1 you’d add a loop where you do a Get-ChlidItem on that directory, and for each class definition you dot source it?  
Then it’s technically getting sourced in each psm1 file, but you only have to maintain it in one place.

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [July 31, 2024, 2:10pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/7 "2024-07-31T14:10:03Z")

</div>

It does sound kind of hacky, but worth a try.

It just seems odd that PS would have classes, but no best-practice way to implement them across a PS module.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [July 31, 2024, 4:53pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/8 "2024-07-31T16:53:20Z")

</div>

I am sure you both did the google thing, but just in case, will this solve your problem? Not a user of classes myself but always willing to learn.

> <https://stackoverflow.com/questions/78070336/how-to-load-the-classes-in-powershell-module-globally>

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [July 31, 2024, 7:51pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/9 "2024-07-31T19:51:07Z")

</div>

I think I got this to work. I used code from here in my root module:

> **[about Classes - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.4#exporting-classes-with-type-accelerators)**
>
> Describes how you can use classes to create your own custom types.

Then I put my class definitions in a ps1 file in my module folder structure and dot sourced it in the root psm1 file.

For pester testing, I had to also dot source the relevant class files in each function’s psm1 and Tests.ps1 file. So far it seems to do the trick.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 30, 2024, 7:51pm UTC](https://forums.powershell.org/t/powershell-classes-in-a-module/24513/10 "2024-08-30T19:51:56Z")

</div>

Closed on August 30, 2024, 7:51pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
