Using CSV File in Module

This topic contains 2 replies, has 2 voices, and was last updated by  Greg Tate 1 year, 3 months ago.

  • Author
  • #63102

    Greg Tate

    I would like one of my internal functions to import a CSV file that exists within the module directory. What's the best way to do this?

    I see there's a spot in the module manifest file where I can include the CSV file in the file list. But how would I go about accessing that file?


  • #63103

    Don Jones

    There's a magic variable called $MyInvocation that has a number of properties you can access, including the location of the current script. We cover it pretty well in "The PowerShell Scripting and Toolmaking Book," if you have that. Given that location, you just use Import-CSV or whatever.

    • #63105

      Greg Tate

      Thanks Don. I checked out MyInvocation, but it didn't have the properties I was after.

      However, I just found a property of the module itself called $MyModule.ModuleBase that gives me the path to the module directory. From there I can check for the CSV file within that directory:

      $module = Import-Module MyModule -passthru
      Test-Path -Path "$($module.modulebase)\mycustomfile.csv"

You must be logged in to reply to this topic.