OneDrive installation detection, process start

Samuel Lindsay 1 Reputation point
2022-10-24T01:30:14.22+00:00

Hi guys,

Working on a script to restart OneDrive depending on installation directory. I've managed to throw a bit together but it the final Smart-Process doesnt know which variable is right.

Code below.

Thanks!

Checks for installation location.

$InstallationLocation = (Test-Path -Path "C:\Program Files\Microsoft OneDrive\OneDrive.exe")
if (Select-String -Pattern "True" -Quiet -InputObject $InstallationLocation)
{

Program Files

$InstallationType = "Program Files"  

}
elseif (Select-String -Pattern "False" -Quiet -InputObject $InstallationLocation)
{

AppData

$InstallationType = "AppData"  

}

if (Select-String -Pattern "Program Files" -Quiet -InputObject $InstallationType)
{
$Directory = "%LOCALAPPDATA%\Local\Microsoft\OneDrive\OneDrive.exe"
}
elseif (Select-String -Pattern "AppData" -Quiet -InputObject $InstallationType)
{
$Directory = "C:\Program Files\Microsoft OneDrive\OneDrive.exe"
}

Start-Process -ArgumentList $Directory -wait
Write-host "Installation Type: $InstallationType"
Start-Sleep -Seconds 5

if((get-process "OneDrive" -ea SilentlyContinue) -eq $Null){
echo "OneDrive is Not Running"
}

else{
echo "OneDrive is Running"
}

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-10-25T18:17:45.33+00:00

    I think this is what you're trying to do. However, I think in your script you have the "Program Data" and "AppData" directories reversed. I did not correct that in my version of your code. Please verify that the correct values are placed into the "$Directory" variable.

    $InstallationType = ""  
    $Directory = ""  
    if (Test-Path -Path "C:\Program Files\Microsoft OneDrive\OneDrive.exe") {  
        $Directory = "%LOCALAPPDATA%\Local\Microsoft\OneDrive\OneDrive.exe"     # Program Files  
        $InstallationType = "Program Files"  
    }  
    else{  
        $Directory = "C:\Program Files\Microsoft OneDrive\OneDrive.exe"         # AppData  
        $InstallationType = "AppData"  
    }  
      
    Start-Process -FilePath $Directory -Wait  
    Write-Host "Installation Type: $InstallationType"  
    #Start-Sleep -Seconds 5                             # Why wait????  
      
    if ($null -eq (Get-Process "OneDrive" -ea SilentlyContinue)) {  # always put $null on the left side!!  
        Write-Host "OneDrive is Not Running"  
    }  
    else {  
        Write-Host "OneDrive is Running"  
    }  
    

  2. Peter Louvel 10 Reputation points
    2023-10-03T21:42:15.6833333+00:00

    slightly modified the code to use $env:LOCALAPPDATA and made it start OneDrive in background mode so windows explorer won't start.
    also it won't start OneDrive if it's not installed for any reason

    $Directory = $null
    if (Test-Path -Path "C:\Program Files\Microsoft OneDrive\OneDrive.exe") {  
        $Directory = "C:\Program Files\Microsoft OneDrive"
    }  
    if (Test-Path -Path "$env:LOCALAPPDATA\Microsoft\OneDrive\OneDrive.exe") {  
        $Directory = "$env:LOCALAPPDATA\Microsoft\OneDrive"
    }    
    if ($null -ne $Directory) {
        if ($null -eq (Get-Process "OneDrive" -ea SilentlyContinue)) {
          Start-Process -FilePath "$Directory\OneDrive.exe" -ArgumentList "/background"
      }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.