Home > Blockchain >  Powershell Dynamic path location for files
Powershell Dynamic path location for files

Time:11-08

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'

CodePudding user response:

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.

  • Related