2
votes

I have a Power Shell Module that references a number of PS1 files.

. $PSScriptRoot\vs-command.ps1
. $PSScriptRoot\open.ps1
. $PSScriptRoot\git.ps1
. $PSScriptRoot\build.ps1
. $PSScriptRoot\deploy.ps1
. $PSScriptRoot\count.ps1
. $PSScriptRoot\tab.ps1
. $PSScriptRoot\test.ps1
. $PSScriptRoot\nuget.ps1
. $PSScriptRoot\checkforxml.ps1

This PSM1 file is held in the location of Documents\WindowsPowerShell\Modules and currently ties in to the PS1 files referenced above.

The files above perform a number of actions on visual studio projects \ solutions and i now have a requirement for each of the files to be held in their own folder within the solution folder.

I need to find a way of switching the $PSScriptRoot while the same PowerShell window is open.

So.... the requirement is... initial load of these files so i have the basic commands that I would expect... then... when i switch into a Solution Folder held in d:\slns it will detect the root has changed and then pick up the new ps1 files. .

I am very new to powershell so would be very grateful for all help and information.

thanks

1

1 Answers

0
votes

$PSScriptRoot is an automatic variable with the path of the folder from which the given PowerShell script was started, i.e. your script is dot-sourcing the other .ps1 files from its own parent folder.

If you need to source files from a different location, you need to replace that variable with the respective path to each file.

I'm still not sure I understand your question, but perhaps something like this will do:

switch -wildcard ($PSScriptRoot) {
  '*\somefolder\*' {
    . "$PSScriptRoot\A.ps1"
    . "$PSScriptRoot\..\..\C.ps1"
  }
  '*\otherfolder\*' {
    . "$PSScriptRoot\B.ps1"
    . 'C:\some\where\else\D.ps1'
  }
  default {
    . "$PSScriptRoot\A.ps1"
    . "$PSScriptRoot\B.ps1"
  }
}