Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to organize powershell class definition

I run into issue when executing the following code snippet in ise by selecting all and click run:

Import-Module ActiveDirectory
class ADUtil
{
    [Microsoft.ActiveDirectory.Management.ADGroup] $Group
}

The type [Microsoft.ActiveDirectory.Management.ADGroup] is unknown. If I execute Import-Module ActiveDirectory explicit at first, then I can run the class definition without any error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Correct. Naturally, you have to import the module first, before using any types contained in it.

To make this more usuable, there are 2 possibilities off the top of my head:

  1. If it’s a script, you can use the #Require statement:
#Require -Module ActiveDirectory
  1. If you use this in the console frequently, add the import to your $Profile
Import-Module ActiveDirectory
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading