Using CSV File in Module

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Greg Tate Greg Tate 3 months, 3 weeks ago.

  • Author
    Posts
  • #63102
    Profile photo of Greg Tate
    Greg Tate
    Participant

    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?

    Thanks!

  • #63103
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
      Profile photo of Greg Tate
      Greg Tate
      Participant

      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.