Saturday, December 19, 2015

Symbolic Links in PowerShell V5

I have been working today with a new feature in PowerShell V5 and the ability to create symbolic links. In Windows, a symbolic link is a file system item (i.e. a file or folder) that points to some other file system object (some other file system file or folder). The symbolic link is transparent to the user, although the UI does give some clues if you know where to work.

You can also create a hard link. A hard link is an item in file store whereby more than one path references a file in the same volume.

To create these links, you could do the following:
# Create Test Folder
Set-Location c:\Foo\SlTest
mkdir C:\Foo\SlTest
#
# Now a subfolder
mkdir C:\Foo\SlTest\Real
#
# And a real doc
1..10 | Out-File c:\Foo\SlTest\Real\foo.txt
#
# Now create a symbolic link
New-Item -ItemType SymbolicLink -Name .\virtual -Target .\real  
#
# And a symlink for a file
New-Item -ItemType SymbolicLink -Name .\SYMfoo.txt  -Target .\real\foo.txt
#
# You can also create a hard link
New-Item -ItemType HardLink     -Name .\hard.txt  -Target .\real\foo.txt
Once you run this (suitably amended for your environment possibly), you would see some thing like this in Explorer and within a PowerShell V5 window (nb: I am running V5 on a Server 2012R2 system with the RTM version of WMF 5).



PowerShell shows the three links in the directory listing (note  the 'l' in the mode column). Windows Explorer, however, only shows two link symbolic links. Explorer does not show an 'L' attribute for the hard link. The hard link is kind of cool, in that you can't really see it's there from Explorer. It really looks like the file, but as a different name. The symbolic links, on the other hand show up a bit more clearly in Explorer.

I find symlinks particularly useful in my training. For my PowerShell courses, I create a 4-VM 'farm', and use differencing disks to reduce the overlap in contents (Saves around 20 GB!). I create a symbolic link for the differencing disk (ie where it should be)  that points to the one copy of that file I DO copy.  Up to now, my setup instructions require the setup tech to run a batch file using Command.com. Once PowerShell V5 is commonly available, I'll get rid of that and make the setup all PowerShell.


del.icio.us Tags: ,

1 comment:

Unknown said...

The inclusion of support for symbolic links, hard links, and junctions in WMFv5 is the final piece I needed to allow me to ditch cmd forever. I am very, very glad to no longer need to use "cmd /c mklink" to make a hard link. (I use hard links for many different tasks, but especially to allow me to have multiple names calling the same script, and then branch the script based on the name used to call it.)

I find junctions somewhat more useful across drive letter boundaries than symbolic links, but both have their place. And now both are fully supported in PowerShell.

(For those who care, the reason that earlier versions of PowerShell couldn't just directly call mklink is that mklink is an internal command of cmd.exe, not a standalone executable.)