Search code examples
powershellclassnamespaces

PowerShell: Can you write a class like the system ones?


Hopefully a very simple question. I'm diving into classes within PowerShell; What I'm wondering is normally you estantiate a class as Class <myClass> {} is there a way to write it like the system ones System.Collections.Generic..... So it would be something akin to: class <myCompany>.<myClass> {}?


Solution

  • NO

    PowerShell classes cannot be namespaced with native syntax, you'll need to compile your types from C# using Add-Type:

    Add-Type -TypeDefinition @'
    namespace MyCompany {
      public class MyClass { }
    }
    '@