Using CSV File in Module

Welcome Forums General PowerShell Q&A Using CSV File in Module

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 10 months ago.

  • Author
    Posts
  • #63102

    Participant
    Points: 1
    Rank: Member

    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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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

      Participant
      Points: 1
      Rank: Member

      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"
      

The topic ‘Using CSV File in Module’ is closed to new replies.