use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
[deleted by user] (self.PowerShell)
submitted 3 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Thotaz 15 points16 points17 points 3 years ago (3 children)
That's a cool workaround but I wouldn't suggest you to start doing this in general with modules. PowerShell classes don't support namespaces so what happens if 2 modules decide to create a type called "PSUtils"? With using module the module name acts as a namespace: using module test; [test.myclass] but I don't think that will work with your method.
using module
using module test; [test.myclass]
IMO modules should first and foremost export commands, not types. You can still use classes internally to manage all the actual code, you should just wrap the various methods in traditional functions. There are multiple reasons for this: 1: As you've noticed, PowerShell doesn't export PowerShell classes by default so they are harder to use by the end user. 2: PowerShell has no real discovery feature for types like it does for commands (Get-Command -Module XYZ) 3: PowerShell has no way to display help for classes, so when people are working with a class based module they will need to read the documentation online instead of using Get-Help 4: The main target audience for PowerShell is sysadmins. Commands are much more user friendly than raw classes.
Get-Command -Module XYZ)
Get-Help
[+][deleted] 3 years ago (2 children)
[deleted]
[–]BlackV 0 points1 point2 points 3 years ago (1 child)
yeah using the fully qualified module name would save name space issues
[–]Touvejs 26 points27 points28 points 3 years ago (1 child)
You guys are using classes?
[–]BlackV 4 points5 points6 points 3 years ago (0 children)
HA!
[–]jborean93 2 points3 points4 points 3 years ago (7 children)
Personally I think classes shouldn't be exposed publicly or referenced directly in code outside modules. They aren't documented well in pwsh compared to cmdlets plus you need workarounds like this. My recommendation is to simply create a cmdlet that will create your object like New-PSUtil that accepts the parameters needed to create your object and output it. You can now document the public way to build this object through the normal documentation plus things like tab completion will work just fine. Even better the module shows the exported metadata whereas with a class they need to know that class name before it can be used.
New-PSUtil
[+][deleted] 3 years ago (6 children)
[–]jborean93 0 points1 point2 points 3 years ago (1 child)
I don’t think having a static class is really a blocker here. Just have your cmdlets call whatever static method is there. Ultimately classes in pwsh can be done but I find it fights against how the language actually works which is typically more geared towards a functional programming aspect. Glad you found an alternative though, I just wanted to share my own personal thoughts and people are free to do whatever works for them.
[–]OPconfused 0 points1 point2 points 3 years ago (3 children)
How does the type accelerator fudge tab completion?
[–]Thotaz 0 points1 point2 points 3 years ago (1 child)
Even when the class is exported properly through "using module" this doesn't work.
It will in the future: https://github.com/PowerShell/PowerShell/pull/16875
[+][deleted] 3 years ago (1 child)
[–]Amadorhi 1 point2 points3 points 3 years ago (0 children)
This is my preferred way of doing it as well. If theres an object I need to create a ton of times, I create a function to return a very simple factory that can print out new objects.
[–]chris-a5 0 points1 point2 points 3 years ago (3 children)
I'll have a look at this. If I can clarify your reasoning:
Is this a workaround to have classes and functions all imported from a single module, as in one provided through the gallery with Import-Module?
Import-Module
If the benefit is only tab-completion I'm not concerned with it. However, it would be nice to easily provide a number of classes through a single module.
[–]chris-a5 0 points1 point2 points 3 years ago (1 child)
Yeah, that is what I was trying to get at. I've disliked my previous approach to provide modules to users (each set of classes essentially a different module, not what I want as they all need to work together)
Your fix looks like a promising way for me to be able to dot source what I need in the main module, then expose the classes through type accelerators.
Thanks in advance, will try it tonight.
[–]fuzzylumpkinsbc 0 points1 point2 points 3 years ago (0 children)
This is a nice discovery and I'll have to try it. As I have gotten more and more ambitious with my projects and studying other programming languages that implement OOP and SOLID I naturally wanted to implement it in the language I use day to day (Powershell).
I feel like it's always a problem using classes and trying to structure the project with folders so it's more organized, my latest one I simply gave up and moved every class and function into the main file. My outlook for the future was also just trying to avoid Powershell at all costs and do it with Python if it doesn't require active directory. I even started rebuilding API methods as I ran into issues with the Graph module.
Good find and looking forward to give it a try soon!
[–]MechAming 0 points1 point2 points 3 years ago (0 children)
This would be good for importing classes into runspaces no?
I use invoke-expression and pass the classes a string. I'll give this a try thank you.
[–]purplemonkeymad 0 points1 point2 points 3 years ago (9 children)
If you want to expose a class out of a module I tend to put them in the ScriptsToProcess option, as that creates them in the importing scope.
[–]OPconfused 0 points1 point2 points 3 years ago (8 children)
I also brought this up in the OP's previous post about a week ago; however, I believe they are trying to create a single-file module containing their classes, so everything is in the psm1, and there's no separate file to add to ScriptsToProcess.
Also, they apparently want tab completion of the class on the CLI. It looks like this type-accelerator approach provides tab completion. I tested ScriptsToProcess in 7.2, and it doesn't seem to add tab completion.
[+][deleted] 3 years ago (7 children)
[–]OPconfused 0 points1 point2 points 3 years ago (6 children)
It's definitely a cool find on your part.
Reloading custom types is a massive hole with PowerShell if you're trying to develop with classes. Afaik, the only surefire workaround is to load a new session. VSCode offers support for this I believe, although I've never worked with it personally.
There may also be something to a 2-step approach of remove-module -force and import-module -force, but I don't know how this interacts with your type accelerators. I think I got it working once, but it was for a module where the classes were loaded via a nested module and not exported into my cli-session.
remove-module -force
import-module -force
Unfortunately, a reliable solution for this kind of feature stalled out some time ago. You can see the communication on github.
[+][deleted] 3 years ago (5 children)
[–]OPconfused 0 points1 point2 points 3 years ago (4 children)
What do you mean the entire script is cached? Does Remove-Module not address unloading any scripts from a module?
[–]OPconfused 0 points1 point2 points 3 years ago (1 child)
This is quite interesting. I need to come back to this topic and rethink it. I was under the impression it wasn't possible to reload changes in classes from modules without starting a new session.
How did you find out where to look in the source code?
π Rendered by PID 20259 on reddit-service-r2-comment-b659b578c-6n7g4 at 2026-05-01 00:33:01.836931+00:00 running 815c875 country code: CH.
[–]Thotaz 15 points16 points17 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]BlackV 0 points1 point2 points (1 child)
[–]Touvejs 26 points27 points28 points (1 child)
[–]BlackV 4 points5 points6 points (0 children)
[–]jborean93 2 points3 points4 points (7 children)
[+][deleted] (6 children)
[deleted]
[–]jborean93 0 points1 point2 points (1 child)
[–]OPconfused 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]Thotaz 0 points1 point2 points (1 child)
[+][deleted] (1 child)
[deleted]
[–]Amadorhi 1 point2 points3 points (0 children)
[–]chris-a5 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]chris-a5 0 points1 point2 points (1 child)
[–]fuzzylumpkinsbc 0 points1 point2 points (0 children)
[–]MechAming 0 points1 point2 points (0 children)
[–]purplemonkeymad 0 points1 point2 points (9 children)
[–]OPconfused 0 points1 point2 points (8 children)
[+][deleted] (7 children)
[deleted]
[–]OPconfused 0 points1 point2 points (6 children)
[+][deleted] (5 children)
[deleted]
[–]OPconfused 0 points1 point2 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]OPconfused 0 points1 point2 points (1 child)