Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all references to a given PowerShell module

Tags:

powershell

Is there a way to find a list of script files that reference a given module (.psm1)? In other words, get all files that, in the script code, use at least 1 of the cmdlets defined in the module.

Obviously because of PowerShell 3.0 and above, most of my script files don't have an explicit Import-Module MODULE_NAME in the code somewhere, so I can't use that text to search on.

I know I can use Get-ChildItem -Path '...' -Recurse | Select-String 'TextToSearchFor' to search for a particular string inside of files, but that's not the same as searching for any reference to any cmdlet of a module. I could do a search for every single cmdlet in my module, but I was wondering if there is a better way.

Clarification: I'm only looking inside of a controlled environment where I have all the scripts in one file location.

like image 665
Nathan W Avatar asked Jun 09 '26 17:06

Nathan W


1 Answers

Depending on the scenario, the callstack could be interesting to play around with. In that case you need to modify the functions which you want to find out about to gather information about the callstack at runtime and log it somewhere. Over time you might have enough logs to make some good assumptions.

function yourfunction {
    $stack = Get-PSCallStack
    if ($stack.Count -gt 1) {
        $stack[1] # log this to a file or whatever you need
    }
}

This might not work at all in your scenario, but I thought I throw it in there as an option.

like image 65
HanShotFirst Avatar answered Jun 11 '26 17:06

HanShotFirst