# POWERSHELL DOES NOT WANT TO LOAD CLASSES

**URL:** <https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202>\
**Category:** PowerShell Help\
**Created:** [February 25, 2018, 11:44pm UTC](https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202 "2018-02-25T23:44:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![syobsyot](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/syobsyot/32/374_2.png) [@syobsyot](https://forums.powershell.org/u/syobsyot)\
**Post date:** [February 25, 2018, 11:44pm UTC](https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202/1 "2018-02-25T23:44:54Z")

</div>

HELLO PEOPLE,

I HAVE ONE PROBLEM WHILE USING POWERSHELL. SOMETHING STRANGE HAPPENS. AND THIS IS THAT POWERSHELL DOES NOT WANT TO LOAD CLASS I WROTE. BUT ONLY IF I TRY TO LOAD IN POWERSHELL.EXE. IF I TRY TO LOAD IN POWERSHELL\_ISE.EXE, IT WORKS.  
HERE IS ONE EXAMPLE:  
IF I OPEN POWERSHELL\_ISE.EXE AND CREATE NEW SCRIPT, FOR EXAMPLE TEST.PS1, AND I WRITE INTO SCRIPT FOR EXAMPLE:

```
CLASS TEST {}
```

AND THEN IF I SAVE THIS SCRIPT, AND AFTER THAT TRY TO RUN FOLLOWING COMMANDS IN POWERSHELL\_ISE:

```
TEST.PS1;
[TEST]::NEW();
```

EVERYTHING WORKS OKAY.

BUT THE PROBLEM IS THAT IF I OPEN POWERSHELL AND RUN EXACT SAME COMMANDS IN IT:

```
TEST.PS1;
[TEST]::NEW();
```

IT DOES NOT WORK, BUT INSTEAD COMPLAINS THAT TYPE TEST IS NOT DEFINED.

THIS IS ODD, SINCE IT WORKS IN POWERSHELL\_ISE. AND IT SHOULD WORK SINCE I RUN TEST.PS1; LINE WHICH SHOULD DEFINE NEW TYPE [TEST]. I SAW ON INTERNET EXAMPLES OF PEOPLE CLAIMING THAT IT WORKS IF YOU USE USING MODULE COMMAND AND IF YOU SAVE CLASS DEFINITION INTO .PSM1 FILE.  
SO, I HAVE TRIED RENAMING SCRIPT TO BE TEST.PSM1 AND THEN RUNNING:

```
USING MODULE TEST.PSM1;
[TEST]::NEW();
```

BUT STILL, I GET THE SAME ERROR. POWERSHELL SIMPLY DOES NOT SEE CLASS DEFINED IN TEST FILE.

SO, MY QUESTION IS, IS THIS NORMAL, OR SOMETHING IS WRONG WITH MY POWERSHELL? SINCE ON INTERNET I SEE PEOPLE SAYING IT WORKS.

OR, SHOULD I DO SOMETHING DIFFERENT IN ORDER FOR IT TO WORK?

FOR ANY POTENTIAL REPLYS, THANK YOU.  
BYE

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [February 26, 2018, 8:31am UTC](https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202/2 "2018-02-26T08:31:36Z")

</div>

Starting ISE

create a class

class test {  
[int]$P1 = 1  
[int]$p2 = 2

}

Run the code

you can create an instance of the class in the ISE Script pane  
PS\> test::new()

P1 p2

* * *

1 2

Now save the code as test.ps1.  
Run the code in the console

PS\> .\test.ps1  
PS\> test::new()  
Unable to find type test.  
At line:1 char:1

- 
- 

```auto
  + CategoryInfo : InvalidOperation: (test:TypeName) [], RuntimeException
  + FullyQualifiedErrorId : TypeNotFound

```

The reason you’re not seeing test is that a script runs in its own scope and all variables, functions classes etc are removed at the end of the script’s execution.

You need to dot source the script so that the class remains in memory. You do that by putting a . in front of the script name like this

PS\> . .\test.ps1  
PS\> test::new()

P1 p2

* * *

1 2

---

<div class="post-metadata">

**Author:** ![syobsyot](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/syobsyot/32/374_2.png) [@syobsyot](https://forums.powershell.org/u/syobsyot)\
**Post date:** [February 28, 2018, 7:53pm UTC](https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202/3 "2018-02-28T19:53:20Z")

</div>

HELLO RICHARD,

THANK YOU VERY MUCH FOR CLARIFYING THIS TO ME.  
I AM SORRY, THIS WAS MY BAD. I GUESS I MISSED PART ABOUT DOT SOURCING WHEN I WAS STUDYING POWERSHELL.

THANK YOU.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/powershell-does-not-want-to-load-classes/10202/4 "2024-05-16T20:36:18Z")

</div>


