0

I'm trying to make a script that installs apps etc. the script contains 7 ps1 scripts and they are linked together but when I move the folder the script won't work since the path changed is there a way so I can always have the right path?

& 'Z:\Windows installatie\Scripts\Menus\Apps.ps1'

this is when it's from a USB but the drive letter always changes.

I tried using a wild card but that didn't work.

& '*\Windows installatie\Scripts\Menus\Apps.ps1'

Ismaili Mohamedi
  • 906
  • 7
  • 15
smurfexe
  • 11
  • 2
  • Use relative paths for the linked scripts. As long as they are in the same directory structure, the main script will find them. As for calling the main script, yes, if you change the location you would have to adjust the line with which you call it accordingly – Theo Nov 07 '22 at 13:52
  • You can use the wonderful and free "USB Drive Letter Manager" (USBDLM) to force your USB drives to always use the same drive letters. I use this for my backup drives so Macrium Reflect can always find them. – RetiredGeek Nov 07 '22 at 15:40

1 Answers1

0

If you would like to test for the existence of a folder or file under an unknown drive letter, and you know the path is going to be unique enough, then you could just test for it by iterating through Get-PSDrive -PSProvider FileSystem.

$Drive = Get-PSDrive -PSProvider FileSystem | Where-Object { Test-Path ($_.Root + "path\to\myScript.ps1") }
if ($null -ne $Drive -and $Drive.Count -eq 1) {
    & (Join-Path -Path $Drive.Root -ChildPath "path\to\myScript.ps1")
}

Not incredibly elegant, but will do the job.

If you know that you only have one USB mounted at a time, then you could also check for details about the drive that is a USB.

Ash
  • 3,030
  • 3
  • 15
  • 33