Using PowerShell to Run a Script that installs some files from a mapped drive

293 views Asked by At

I have a folder that has some install files for new user computers and need to create a script that runs the install files from a folder that is located on a network drive that's mapped to my computer.

I'm running the below script from PowerShell but I'm getting an error

Join-Path : Cannot find drive. A drive with the name 'Z' does not exist

I'm running as admin and I've set the execution policy to remote signed so it allows me to run the script.

Below is the start of the script. Im testing one exe. file and will add more once I actually get this to run and perform a successful install.

$folderPath ="Z:\HD\NEW PC SET UP\chrome"

$exeFileName = "ChromeSetup.exe"

$exePath = Join-Path -Path $folderPath -ChildPath $exeFileName

if(Test-Path $exePath -PathType Leaf){
    Start-Process -FilePath $exePath
} else {
    Write-Host "Executable not found: $exePath"

}

From what I gathered so far it seems I need to remap the network drive to an unused letter and then access it as a normal drive ? Not sure if I'm going down the right path on this.

nothing yet as im not sure if remapping the drive in the script is the proper way to do this

1

There are 1 answers

0
SE1986 On

Drive mappings are per user and running as an admin counts as being a different user.

Open a powershell prompt as your account (not elevated) and run the following command and you should see your Z: drive:

get-psdrive -PSProvider Filesystem

if you then run the same command in your elevated command prompt, you will not see any shares

to fix this, you need to add the share using New-PsDrive (after checking it doesn't already exist)

Alternatively, Powershell supports UNC paths so you could do away with the drive mapping altogether