I had a conversation with a colleague the other day who wasn't aware of the power of Symbolic Links. They're both a useful tool that's not often talked about for Windows, and a thing that doesn't work quite as easily as the documentation might imply.
So what are these?
url copied!
A Symbolic link (or "symlink") is a way of making a file or directory appear in multiple places in your filesystem without actually copying it. You can think of it like a directory entry in your filesystem that's a pointer to a different entry, rather than being a file or directory itself. But unlike a shortcut file, it doesn't appear as a different file type - it's transparently a link instead.
For example, if you have folder of source files that are used by multiple projects, you might share that folder in each of your projects with a symlink:
flowchart LR
subgraph sh[Shared folder]
f1[File One]
f2[File Two]
end
p1[Project One]
p2[Project Two]
p1 --Folder
SymLink--> sh
p2 --Folder
SymLink--> sh
So there's only one copy of the shared folder on disk, but both of the projects include this folder. And any changes to the files in that folder are immediately seen by these two projects.
Using them
url copied!
If this sounds like a thing you'd want to use then you might do a search to find out how to create these on Windows. And the answer that generally comes back from web searches is that there's
a command-line tool
called
mklink
that allows you to set up these links.
But what most of the answers you'll find as I type this miss out, is that
mklink
doesn't work from a PowerShell session. (And I find myself doing most of my cli admin from that these days) It's not actually a separate program that you can call - it's a built-in command of the old command interpreter. So if you open an old-style command prompt it will work, but PowerShell will say that command doesn't exist:
That can be quite frustrating, since most of the documentation that pops up when you search about this doesn't explain that little quirk.
So, you have two choices:
- You can make sure you open a "Command Prompt" not a PowerShell window
That's true of both the old-style separate windows, and the more modern Terminal app which can host both. - There's a different command to use in PowerShell
It makes use of theNew-Itemcommandlet to provide support in your.ps1files and immediate scripting.
Note that these commands require admin privileges - so you need to use "run as administrator" to start the command prompt or PowerShell session.
In both cases, you need to specify two things:
- The name (or path) of the file or folder that will be the link
- The full path of the file or folder to link to
So if you're in the "Project One" folder and want to create a folder called "shared" which links to the "sharedcode" folder, you could use:
-
mklink /D "shared" "c:\wherever\sharedcode" -
New-Item -ItemType SymbolicLink "shared" -Target "c:\wherever\sharedcode"
Easy when you know how - but can be a bit tricky of you're new to it...
↑ Back to top