This blog post is continuation of series of posts on understanding classes in PowerShell. Part 06 of series can be read at here. If you have defined PowerShell class inside a module, its not easy to import class in the current session. By default, it does not load classes. Let’s understand the details of it.
Import-Module does not import Classes
Import-Module has been a standard way of importing functions defined in a PowerShell module. It also comes with -verbose parameter so that you can see what has been imported. For understanding the case with classes, let’s define a simple PowerShell module HelloModule.psm1 as below code: Continue reading “Import PowerShell Classes from modules”
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 05 of series can be read at here. In previous blog post, we have learned about meaning of inheritance, define child class and parent class, overload methods, override methods of base class and calling base class members anyhow.
Effect of inheritance on constructors
When we discussed about inheritance, we said that all properties and methods of a base class are inherited. However, constructors (check here on what are constructors and how to define them), are not inherited by a child class. Instead, like all PowerShell classes, the new class is created with a default (parameter-less) constructor. If we think about it, this makes sense after all. The purpose of a constructor is to initialize the object associated with a class. …Continue Reading
I have enabled few site related changes which you may find more useful:
- Instead of showing full blog post at the home page, there will be a detailed excerpt of few lines and it will be followed by a shortcut leading to full blog post. Over the time, the blog has grown too large to show everything on a single page. So you don’t have to keep scrolling to see post of your interest.
- If you open a blog post, there is now related content at bottom of the post. This will help in finding related content more easily on this blog.
Let me know your thoughts on same. I would love to hear from you.
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 04 of series can be read at here.
What is Inheritance
In many classes that you create, you will find that you often need the same properties and methods from another class that you created earlier. For example, if you have a base class called the Person class, and it contains LastName and FirstName properties and a Print method, you will find that for an Employee class, you need the same properties and methods. You may also need additional properties, such as EmployeeID and Salary. When you inherit from the Person class (the base class) you can add these properties to this new Employee class, and still have access to all of the properties in the Person class. Inheritance is about the ability for one class to define itself as having all the properties and methods of a particular class, and then extending the definition of the base class by adding additional properties and methods.
Why should we use Inheritance
Inheritance is desirable because you want to avoid writing the same code over and over again. If you have two separate classes, and each one …Continue Reading
If we need to know, which PowerShell host version is being used, we can use
(Get-Host).name in PowerShell:
PS C:\Windows> (get-host).name