Help with powershell script to fetch multistrings


I have made a powershell script, which does some add/delete things locally on a client with MS onedrive problems. Please see the script attached.
I succesfull tested the script on my own client and that fine, but now i found out that the local sync location path is different and uses different locations and pathnames on all company clients and therefore i need to add some code to my script which does the following:

  1. Read the following stringdata regkey

Path: HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation
Data: C:\Users\dfbdp\OneDrive - Danske Fragtmænd A-S

  1. Insert this datavalue (path of folder) into the "Move-Item “C:\Users$env:username\OneDrive - Danske Fragtmænd A-S” “C:\Users$env:username\Desktop\Onedrive_Backup”…
    in the script so i basicly read the location path from the regdatabase instead.

I have tried to solve this problem my selv with the Get-RegistryKey etc but have not been able to make it work.

I am not that skilled in this, so can anyone help me?

Best regards

If you type in this:

Set-Location -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation"

What do you get then?

Best regards Alexander

I am not that skilled, so i dont quite follow you. Where do i insert this? into the script or?

Execute it from a Powershell console with elevated privileges.


Ok i get this:

[b]PS C:\Users\dfbdp> Set-Location -Path “HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation”
Set-Location : Cannot find path ‘HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation’ because it does not exist.
At line:1 char:1

  • Set-Location -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSy …
  •   + CategoryInfo          : ObjectNotFound: [HKCU:\Software\...entDiskLocation:String] [Set-Location], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand[/b]

Ok, then change the path to “HKCU:\Software\Microsoft\Office\15.0\Common\Internet” instead.
What is returned now?


Also I would like to suggest that you try this command and tell me what you get in return:

Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation"


Returns this:

PS HKCU:\Software\Microsoft\Office\15.0\Common\Internet>

The Get-Item… returns this:

[b]Get-ItemProperty : Cannot find path ‘HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation’ because it does not exist.
At line:1 char:1

  • Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\Loc …
  •   + CategoryInfo          : ObjectNotFound: [HKCU:\Software\...entDiskLocation:String] [Get-ItemProperty], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand[/b]

and the Get-Item … without “LocalSyncClientDiskLocation” returns this:

UseRWHlinkNavigation :
UseRWOSHlinkNavigation : 0
UseOnlineContent : 2
LcfemCleanShutdown : 0
DoNotCheckIfWordIsDefaultHTMLEditor : 1
LocalSyncClientDiskLocation : {C:\Users\dfbdp\OneDrive - Danske Fragtmænd A-S}
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common\Internet
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common
PSChildName : Internet
PSDrive : HKCU
PSProvider : Microsoft.PowerShell.Core\Registry

Where i can see the line i need in LocalSyncClientDiskLocation : {C:\Users\dfbdp\OneDrive - Danske Fragtmænd A-S}


what then?

Ok, now try the following command:

Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet" | Select-Object  -ExpandProperty LocalSyncClientDiskLocation

Alternatively this:

(Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation


It returns:

C:\Users\dfbdp\OneDrive - Danske Fragtmænd A-S

Thats great, now you got the path that you want!
Do you require any further assistance?


Yes, how do i insert this path into the script then?

into this line marked bold: Move-Item “C:\Users$env:username\OneDrive - Danske Fragtmænd A-S” “C:\Users$env:username\Desktop\Onedrive_Backup”

There is a couple of ways you could do it, I would recommend you to try the following:

$sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
Move-Item -Path "C:\Users\$env:username\$sourcePath" -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"


Hi Alexander,

Ok, i will test this right now and reply on the results asap. Thx

I need to correct a mistake from my side though.

You would like to try this instead:

$sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
Move-Item -Path $sourcePath -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"

Since it returns the full path. Sorry!


Hi Alxeander,

It does not work, but as i read it the exampel you made does this, right?:

$sourcePath = (Get-ItemProperty -Path “HKCU:\Software\Microsoft\Office\15.0\Common\Internet”).LocalSyncClientDiskLocation
Move-Item -Path “C:\Users$env:username[b]C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S[/b]” -Destination “C:\Users$env:username\Desktop\Onedrive_Backup”

Should it not be like this or:

$sourcePath = (Get-ItemProperty -Path “HKCU:\Software\Microsoft\Office\15.0\Common\Internet”).LocalSyncClientDiskLocation
Move-Item -Path “$sourcePath” -Destination “C:\Users$env:username\Desktop\Onedrive_Backup”


Read my latest answer :slight_smile:


Hi alexander.

I allmost there. :slight_smile:
It works, but when i test the script it maked a new folder called “OneDrive - Danske Fragtmænd A-S 1” with a 1 number at the end… and i cant figure out why this is happening… I will test some more and get back to you.
Thank you so much for your help until now.

No problem, if you need further assistance you know where to find me!