Author Posts

January 31, 2017 at 2:55 pm

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!

January 31, 2017 at 3:07 pm

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.

January 31, 2017 at 3:22 pm

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"