I have a C# PSCmdlet class for implementing a PowerShell command and I want to get my module version while running the command.
I don't want to get the version from the assembly location because I need the actual version loaded (it can be different, for example, if I keep PowerShell open while upgrading my module, the assembly will point to the upgraded version and I won't get the one that already loaded).
I need something like Get-Module for the current session but from my C# command code.
How can I do it?
As I've been on the same crusade almost one year later, there's what I came up with:
$version = Split-Path -Leaf $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase
This worked for me:
$MyInvocation.MyCommand.ScriptBlock.Module.Version
It is based on the answer with ModuleBase, but that works only if the version number is the last part of the path, which is not always guaranteed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With