Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - How to get the PowerShell module version from inside the module code at runtime

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?

like image 784
Tali Rivkin Avatar asked Oct 15 '25 02:10

Tali Rivkin


2 Answers

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
like image 192
Jan De Dobbeleer Avatar answered Oct 17 '25 14:10

Jan De Dobbeleer


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.

like image 43
Froggy Avatar answered Oct 17 '25 14:10

Froggy