After an emergency at my previous webhost, I’ve switched to a new webhost. If you see any missing content, let me know!
Dude, Where’s my Command?
The Dilemma
This post was inspired by a recent occurrence at work. I have built a framework which constructs documents based on a lists of functions in a module specific to that kind of document.
I found myself running into an issue where even though I knew there was a command named a certain thing, and that the function was correctly exported from the module, PowerShell wasn’t finding the command.
My code looked something like this:
$module='Module1'
$Steps='Get-CustomerData','Get-MarketingData','New-CustomerMarketingDocument'
# each function takes a hashtable as a parameter and outputs a hashtable
$state=@{}
foreach($Step in $Steps){
$stepCmd=Get-Command -Name $step -module $module
$state=& $StepCmd -state @State
}
So why was PowerShell not working nicely for me? The problems has to do with how PowerShell loads functions. Let’s take a step back
PowerShell’s Command-Loading Procedure
For now, let’s assume we have a simple script module called Module1.
For a script module to be “discoverable” by PowerShell, it must exist in one of the folders listed in the PSModulePath environment variable.
This PowerShell will show you the folders that PowwerShell will look in:
$env:PSModulePath.Split(';')
Here are the results from a PowerShell 7 instance: