Saturday, 25 November 2017

Powershell Symlink to Onedrive

I have more than one PC. To be precise, and overly pedantic, 3 Windows 10 laptops. All are slightly different and of various ages. One I use with a Windows 10 Insider Build to keep up with the next thing coming down the pipeline. On each, I do a bit of Powershell.

Powershell is the latest generation of Windows command line tools. The Linux enthusiast would look down on the Windows command line of whatever generation but Microsoft has always provided command line tools.

In the early days MSDOS was a command line OS. It did little but create directories (folders), copy, move or delete files. It ran “” which some said was just a basic file system manager and way of loading programs into memory. It wasn’t difficult to disagree. Developers brought out alternatives to this such as 4DOS, literally “for dos”, by JP Software. They also produced 4NT, which enhance the Windows NT command processor and 4OS2. The latter was for the IBM OS2 Operating System of the late 80s and early 90s. Years ago I visited JP Software, then based in Boston, and got to meet Tom Rawson who ran the whole thing at the time. Although to be fair I should add the principle developer was Rex Conn.

However, I digress quite considerably. The main point is that the command line, for many users of Microsoft operating systems, became quite powerful and many attempts were made to turn it into a scripting tool. The advantages of scripting, as opposed to programming, was that regular administrative tasks on a PC could be automated. Engineers could set up processes that repeated steps at a specific time or over a range of machines.

Microsoft got the scripting bug too. VBSCRIPT became a scripting language used by many. However, it was a bit clumsy because it was tied closely to the Windows GUI and not really to the command line. This often meant an uneasy use of object orientated programming in places where engineers wanted simple procedural choices to get stuff done quickly.

This brought the world to Powershell. Microsoft’s command line interface that allowed you just to type commands, process text files, setting up common configurations and much more. Mostly it let an engineer write a few lines of commands that did everyday tasks quickly. It was also extended into many of Microsoft’s core business products like Sharepoint, Exchange and Lync. It is everywhere and having a bit of Powershell knowledge is a good thing.

Keeping my personal Powershell library of stuff easily backed up and automatically updated has been a challenge. By default a folder is created at “c:\users\username\documents\windowspowershell” and your startup scripts are dumped there. (You replace “username” in the example with your own login name in Windows to find this location because I am doing a generic example here).

If you install Onedrive sync on your PC then you end up with a path to your documents of “c:\users\username\onedrive\documents”. The obvious issue is that your Powershell settings and default script location is local rather than synced in the cloud. The easy solution to syncing everything up is to do what Unix/Linux guys have done for years – symbolic links. You put a link in your local document folder pointing to Onedrive.

1. Run cmd.exe as administrator.
2. Go to “c:\users\username\documents\” on the command line and rename “WindowsPowershell” folder. Use the RENAME command. Use any name you like.
3. Once the folder is renamed create a symbolic link with the command “mklink /D WindowsPowerShell C:\Users\username\OneDrive\Documents\WindowsPowerShell”

That’s it. Your local Powershell reference actually points to Onedrive now. Remember “username” in this example has to be replaced with your actual computer username.

Repeat on all your PCs and your Powershell scripts will follow you around your PCs.