Search code examples
powershellpowershell-3.0

Look for PS Modules path and make a folder in that path


I'm creating a powershell script that will create a folder for a module in the PS modules folder, then download the module from github. I'm avoiding using the System32 folder, so generally I'd use the ProgramFiles folder, but not everyone has access to that, so the next one would be in the Documents folder for the user running the script. The problem is if that user changed their documents folder from the C drive to another drive, the script will fail, so I'd like to get a command that will take the first path as a string, before the semi colon, when executing

$env:psmodulepath

and use that string to further make module folders.

tl;dr: How do I capture a string starting from 0 up until a semi colon.

Thanks


Solution

  • You can use the -split operator and then take the first element of the array off of the result. The first element in the array is at index 0, which is why you use [0].

    # ("one;two;three" -split ";")[0]
    one
    

    In your scenario, it would probably be something like this:

    ($env:psmodulepath -split ";")[0]