Noob Question - - How to call InvocationInfo.PSScriptRoot for a PowerShell Module

John McCrae 0 Reputation points
2023-09-05T12:15:39.5466667+00:00

I am teaching myself C# by building a PowerShell module. I want to retrieve the PSScriptRoot property from the InvocationInfo class but it appears to be something that is called indirectly. Does anyone have guidance on a) how to invoke it or b) how to use it to spit out the script root?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,463 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rafael da Rocha 5,166 Reputation points
    2023-09-13T18:01:26.99+00:00

    You can use the automatic variable $PSScriptRoot anytime a script is running.
    If you want to see the output, just

    Write-Host $PSScriptRoot
    

    Or $MyInvocation.PSScriptRoot , that will have information about the calling script and not the current script

    Additional information about all the automatic variables is available on Learn:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.