Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: find file name where function is defined

Tags:

powershell

Given the function object (i.e. $f = gi Function:\myfunc) is it possible to find file name and other info (for instance line number) of the file where function is declared?

like image 256
majkinetor Avatar asked May 10 '14 12:05

majkinetor


People also ask

How do I search for a file name in PowerShell?

Get-ChildItem cmdlet in PowerShell is used to get items in one or more specified locations. Using Get-ChildItem, you can find files. You can easily find files by name, and location, search file for string, or find file locations using a match pattern.

How do I check the path in PowerShell?

The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element.

What is $_ used for in PowerShell?

$_ is an alias for automatic variable $PSItem (introduced in PowerShell V3. 0; Usage information found here) which represents the current item from the pipe.

How do I search for a file in a directory in PowerShell?

To search the content in the file in PowerShell, you need to first get the content from the file using Get-Content command and then you need to add Select-String pipeline command. In the below example, we need to search the lines which contain the Get word. You can use multiple patterns to search for the result.


1 Answers

You are on the right track with gi. The ScriptBlock member of the function object has File and StartPosition among its properties:

(gi function:\prompt).ScriptBlock.File

Or you can just go with:

${Function:myfunc}.File
like image 178
Roman Kuzmin Avatar answered Oct 26 '22 01:10

Roman Kuzmin