I’ve got 2 powershell files, one ps1 the other psm1. The code is simple.
personClass.psm1
class Person {
[String]$Name
[int]$Age
# Constructor.
Person([String]$Name, [int]$Age) {
$this.Name = $Name
$this.Age = $Age
}
[String]GreetPerson([String]$PreferredGreeting) {
if($PreferredGreeting -eq "" -or $null -eq $PreferredGreeting) {
$PreferredGreeting = "Hello"
}
return "$PreferredGreeting, $($this.Name). I can't believe you're $($this.Age) years old."
}
[void]HaveBirthday() {
$this.Age++
}
}
File test.ps1
using module ./personClass.psm1
$me = [Person]::new("Garrett", 9000)
Write-Output $me.GreetPerson("Salutations")
$me.HaveBirthday()
Write-Output $me.GreetPerson("Salutations")
When I run this code, I get the error
The specified module ‘C:UsersjreddypersonClass.psm1’ was not loaded because no valid module file was found in any module directory.
Why is this looking in that directory? Shouldn’t the ./ look in the executing directory? I’ve tried a few things and nothing works. If I move my psm1 file to the user directory it works, but that is not an option.