1

Note for those finding this - mklement0's comment worked for me.

The issue is that this .ps1 is a function definition, not a function invocation. The file defines a function but doesn't invoke it.

In order to invoke it from the command line, you need to either add it to your PowerShell environment or remove the function {} wrapper.

One way to add to your environment is to dot-source the file - in this case

. FindGitRepository

which returns with no result, then FindGitRepository. This worked for me in VSCode.

If you want to just run this command (FindGitRepository), you need to either

  1. add it to your profile, etc so it's part of your environment or

  2. remove the function definition and just execute the commands.

===================

Didn't see anything like this in SE question list - there are questions about PS scripts working in ISE but not terminal, but those all seem to be about scripts the OPs are writing that are making system calls, library calls, etc.

This script is not mine, but reading through it does not seem to be making any library calls. Also, why isn't there an error message?

Situation

  1. I'd like to run this powershell script to list metadata on my git repositories - https://jdhitsolutions.com/blog/powershell/4999/finding-git-repositories-with-powershell/

  2. Saved script content as local file c:\tools\Find-GitRepository.ps1 with content.

  3. When I run this in PSE, it completes with results.

 PS C:\tools> Find-GitRepository -path d:\projects\

Repository          Branch   LastAuthor        LastLog                                                               
-----               ------   ----------        -------          
D:\projects\gitpro  main     <correct owner>   8/1/2023 17:18:28                                                              

(expected output, list of git repos. etc)

  1. When I run this in a normal terminal session, admin terminal session, VS Code etc, it completes with no feedback.
PS c:\tools\> powershell.exe .\Find-GitRepository.ps1 -path d:\projects
PS c:\tools> 

(no error thrown or output)

(without leading space)
PS c:\tools> Find-GitRepository.ps1 -path d:\projects
PS c:\tools> 

Suggestion [3,General]: The command Find-GitRepository was not found, but does exist in the current location. PowerShell does not load commands from the current location...

what I've tried that doesn't work

  • tried running in
    • terminal console,
    • terminal admin,
    • vs code
    • a script specifying c:\windows\powershell\7\pwsh.exe --program, etc).
    • running after running

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

all run and return to the c:\tools prompt with no output.

lonstar
  • 308
  • 1
  • 12
  • 1
    Try [Dot sourcing operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.3#dot-sourcing-operator-) as `. .\Find-GitRepository.ps1` and then run the function ```Find-GitRepository -path d:\projects\```. – JosefZ Aug 05 '23 at 18:12
  • As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 7+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Aug 05 '23 at 20:28
  • 1
    Calling a script file (`*.ps1`) - either directly or via `&` - which _contains a function_ (definition) is ineffective if your intent is to _call the contained function_. Instead, you must _dot-source_ (`. c:\path\to\script.ps1`) such a script file first - which ensures availability of the contained function in the current scope - and _then_ call the function (either directly or via `&`). See the linked duplicate for details. – mklement0 Aug 05 '23 at 20:42
  • Thanks! This worked in vscode when I ran the commands sequentially - . FindGitRepository... which returned nothing, then when I ran FindGitRepository...I got a result set back. I am not sure how I can do this in a single command but I guess that's what RTFG is for. – lonstar Aug 06 '23 at 07:50
  • Glad to hear it; it's possible to do it in a single command, but it would be awkward (note the required space after the first `.`): `& $(. .\Find-GitRepository.ps1; 'Find-GitRepository') -path d:\projects` – mklement0 Aug 06 '23 at 18:41

0 Answers0