Start-process to load onedrive.exe from powershell script - cant find the file specified

Hi - attempting to call onedrive.exe using test-path and start-process command. The below script is used when users VPN connect to autoload teams and onedrive. This works for some users but the majority it fails with cannot find file ( see below ). The powershell.exe is called from a VBS logon script ( maps network drives and then calls powershell )

objShell.run(“powershell.exe -ExecutionPolicy bypass -windowstyle hidden -file c:\XXXXXX\admin_scripts\Script_PS_VPN2_Logon_GITDIR.ps1”)

below that then calls this powershell script.

Note the teams component is loaded ok for all users

cd $env:localappdata
.\Microsoft\Teams\Update.exe --processStart “Teams.exe”

if (test-path “c:\Program Files\Microsoft OneDrive\onedrive.exe”) {
cd ${env:ProgramFiles}'Microsoft OneDrive’
start-process -FilePath “OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “c:\Program Files (x86)\Microsoft OneDrive\onedrive.exe”) {
cd ${env:ProgramFiles(x86)}'Microsoft OneDrive’
start-process -FilePath “OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “$env:LOCALAPPDATA\Microsoft\OneDrive\onedrive.exe”) {
cd ${env:LOCALAPPDATA}'Microsoft\OneDrive’
start-process -Filepath “OneDrive.exe” -argumentlist ‘/background’
}

Error that presents is

PS>CommandInvocation(Out-String): “Out-String”

ParameterBinding(Out-String): name=“InputObject”; value=“Cannot find path ‘C:\Program Files (x86)\Microsoft OneDrive’ because it does not exist.”
cd : Cannot find path ‘C:\Program Files (x86)\Microsoft OneDrive’ because it does not exist.
At C:\Zurich\admin_scripts\Script_PS_VPN2_Logon_GITDIR.ps1:8 char:1

  • cd ${env:ProgramFiles}'Microsoft OneDrive’
  •   + CategoryInfo          : ObjectNotFound: (C:\Program File...rosoft OneDrive:String) [Set-Location], 
    

ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
cd : Cannot find path ‘C:\Program Files (x86)\Microsoft OneDrive’ because it does not exist.
At C:\Zurich\admin_scripts\Script_PS_VPN2_Logon_GITDIR.ps1:8 char:1

  • cd ${env:ProgramFiles}'Microsoft OneDrive’
  •   + CategoryInfo          : ObjectNotFound: (C:\Program File...rosoft OneDrive:String) [Set-Location], 
    

ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
cd : Cannot find path ‘C:\Program Files (x86)\Microsoft OneDrive’ because it does not exist.
At C:\Zurich\admin_scripts\Script_PS_VPN2_Logon_GITDIR.ps1:8 char:1

  • cd ${env:ProgramFiles}'Microsoft OneDrive’
  •   + CategoryInfo          : ObjectNotFound: (C:\Program File...rosoft OneDrive:String) [Set-Location], ItemNotFoundE
     xception
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
    
    

PS>TerminatingError(Start-Process): “This command cannot be run due to the error: The system cannot find the file specified.”

Note: the logon vbscript is being called from the netlogon share. I have also tried running the powershell script to load onedrive from the netlogon share also but decided to copy the PS script to the users endpoint device to be called.

the script runs fine and loads onedrive if the user right clicks and selects run with powershell from the script on the users device

thanks for your assistance
Steve

There are missing backslashes in the “cd” commands, it should be as follow :

if (test-path “c:\Program Files\Microsoft OneDrive\onedrive.exe”) {
cd ${env:ProgramFiles}'\Microsoft OneDrive’
start-process -FilePath “OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “c:\Program Files (x86)\Microsoft OneDrive\onedrive.exe”) {
cd ${env:ProgramFiles(x86)}'\Microsoft OneDrive’
start-process -FilePath “OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “$env:LOCALAPPDATA\Microsoft\OneDrive\onedrive.exe”) {
cd ${env:LOCALAPPDATA}'\Microsoft\OneDrive’
start-process -Filepath “OneDrive.exe” -argumentlist ‘/background’
}

Allow me to show you a cleared out version of above script :

if (test-path “${env:ProgramFiles}\Microsoft OneDrive\OneDrive.exe”) {
start-process -FilePath “${env:ProgramFiles}\Microsoft OneDrive\OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “${env:ProgramFiles(x86)}\Microsoft OneDrive\OneDrive.exe”) {
start-process -FilePath “${env:ProgramFiles(x86)}\Microsoft OneDrive\OneDrive.exe” -argumentlist ‘/background’
}

elseif

(test-path “${env:LOCALAPPDATA}\Microsoft\OneDrive\OneDrive.exe”) {
start-process -Filepath “${env:LOCALAPPDATA}\Microsoft\OneDrive\OneDrive.exe” -argumentlist ‘/background’
}

Like this, even if the “Program Files” and “Program Files (x86)” folders should be redirected to another disk or folder, the script will work.

I tested the code above succesfully.

René-Yves,
Welcome to the forum. :wave:t4:

Please, when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Hi Rene

Thanks very much