I'll be honest. I've never been much of a OneDrive user. Even though I'm a great candidate given that I use multiple systems and need access to a common set of files. But for a number of reasons I'm beginning to make more of a shift to OneDrive. Part of the challenge for me has been making it easy to access files in OneDrive. For me, that access might be in a PowerShell session or Windows Explorer. To make this a painless process there are a few options at my disposal.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I want to avoid having to remember where my OneDrive folder is and navigating to it. For the sake of demonstration, I want to make it easy to access the Tools subfolder which contains local copies of the SysInternals suite.
PSDrive
The first option I have is to create a PS Drive in PowerShell. This is very easy.
New-PSDrive -Name Tools -PSProvider FileSystem -Root ~\OneDrive\tools
I am using the console shortcut of ~ which will resolve to C:\Users\Jeff.
I did this because one of the disadvantages of using a PSDrive is that it only persists for as long as my session is running. I'd have to put the New-PSDrive statement in my PowerShell profile script on every computer. But on this computer I might be signed in as 'Jeff'. On another it might be 'jeffh'. Assuming OneDrive is always under C:\Users, I can easily share the same profile among all computers.
One advantage of using a PSDrive is that I can call it anything. I'm not limited to a drive letter.
But this drive isn't available in Windows Explorer or even other PowerShell sessions unless I create via a profile script. Still, this is a valid technique as long as you are aware of the limitations.
Linked Directory
Another option is to create some type of directory link, such as a symbolic link. This is easy to do with New-Item.
New-Item -Path D:\ -Name Tools -ItemType SymbolicLink -Value ~\OneDrive\tools
If you read the help for New-Item you'll see that there are a few options. I tend to like SymbolicLink because it lets me create links across volumes.
Any changes I make to files in D:\tools are reflected in the OneDrive folder and vice versa. This includes creating and deleting files. Plus I can use the folder outside of PowerShell.
The folder is linked as you can see by the icon. Clicking on it opens the OneDrive folder.
If I decide I need to remove the linked directory, in a PowerShell 7 session it just works.
Remove-Item D:\tools
This deletes the linked folder but leaves the OneDrive source alone. However in Windows PowerShell it is a little more problematic. This command should work, but doesn't.
Remove-Item D:\tools -Force -Recurse
You can delete the folder like this:
(Get-Item D:\tools).Delete()
Of if you have the junction utility from Sysinsternals, this would also do the trick.
~\onedrive\tools\junction.exe -d d:\tools
In any case, the OneDrive folder remains. By the way, you could also use junction to create linked folders.
Shortcut Folder
The last technique is an old school approach and that is to create a shortcut link in Windows. You can do this from PowerShell as well using the Wscript.Shell COM object from the VBScript days.
$wshell = New-Object -ComObject "Wscript.Shell"
$link = $wshell.CreateShortcut("D:\Tools.lnk")
$link.TargetPath = "c:\users\jeff\OneDrive\tools"
$link.Description = "Sysinternals Tools"
$link.Save()
This doesn't help me from the console because all I see is a file called D:\Tools.lnk. But in Windows Explorer I can click the link and jump to the OneDrive Tools folder. I can even see the description when I hover over the link. Deleting the lnk file deletes the link. If all I need is an easy way to get to my stuff in Windows Explorer this might be a good technique.
There isn't one perfect technique here. And of course, this is not an exhaustive list. But knowing the pros and cons I can figure out what works for me.
If you move your OneDrive home folder ~\OneDrive won’t map to the new location. I use this PowerShell to resolve the OneDrive location whether it’s been moved or not:
$onedrive = (Get-ItemProperty -Path “hkcu:\Software\Microsoft\OneDrive\” -Name UserFolder).UserFolder
You can get a list of all of your OneDrives with this:
Get-Item -Path “HKCU:\Software\Microsoft\OneDrive\Accounts\*” | select name
That will list all of the accounts. You can grab their UserFolder in each of their keys.
tk
Thanks for the tip. I was using OneDrive as an example and already knew the location. I might use these techniques for DropBox or Box folders. Or even a deeply nested folder I want easier access to. PSDrives are very handy. If I know I need to access that OneDrive registry entry often, I can to this: new-psdrive -name OD -psprovider registry -root “hkcu:\Software\Microsoft\OneDrive\”.
As far as New-PsDrive goes, have you checked out the `-Persist` parameter instead of having to worry about it being session specific?
The persist parameter is definitely an option but your drive name must be a single drive letter. It wouldn’t work with my example of a drive called Tools:
Makes sense if the drive name is a sticking point which form some, that clarity could be pretty important. Still really cool to see all these options. Great article!