Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PSDrives, Shortcuts and Links

Posted on April 15, 2020April 9, 2021

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

onedrive-tools

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.

tools-psdrive

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.

2020-04-15_9-07-35

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.

tools-link

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.

tools-explorer

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

7 thoughts on “PSDrives, Shortcuts and Links”

  1. Todd Klindt says:
    April 15, 2020 at 11:18 am

    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

    1. Jeffery Hicks says:
      April 15, 2020 at 11:23 am

      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\”.

  2. CoffeeCodr says:
    April 15, 2020 at 1:33 pm

    As far as New-PsDrive goes, have you checked out the `-Persist` parameter instead of having to worry about it being session specific?

    1. Jeffery Hicks says:
      April 15, 2020 at 2:11 pm

      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:

      1. CoffeeCodr says:
        April 15, 2020 at 3:07 pm

        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!

  3. Pingback: ICYMI: PowerShell Week of 17-April-2020 | PowerShell.org
  4. Pingback: ICYMI: PowerShell Week of 17-April-2020 – 247 TECH

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d