Recursively find all Symbolic Links with PowerShell

logo-horizontalI just started using SyncThing, and have found it to be a powerful way to keep the files on my various personal computers in sync. I like this solution. I sync on my own private network, avoiding privacy-risking cloud services. I discovered fairly quickly that SyncThing will give warning messages about any symbolic links it encounters, letting you know it won’t try to sync those. In my case, this was shortcut folders like My Videos and My Music in my ~/Documents folder.

powershellThis PowerShell script will let you discover any such symbolic links recursively under a given folder:

param([string]$Path)
if (-not (Test-Path $Path -PathType 'Container'))
{
  throw "$($Path) is not a valid folder"
}
$Current=Get-Item .
function Test-ReparsePoint($File) {
  if ([bool]($File.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
    $File
  } else {
    $FALSE
  }
  return
}
cd $Path
# Recurse through all files and folders, suppressing error messages.
# Return any file/folder that is actually a symbolic link.
ls -Force -Recurse -ErrorAction SilentlyContinue | ? { Test-ReparsePoint($_) }
cd $Current

The function in this script was derived by from the answer given by Keith Hill at https://stackoverflow.com/questions/817794/find-out-whether-a-file-is-a-symbolic-link-in-powershell. If you save the above script as ~/scripts/FindSymLinks.ps1, here is what a sample session looks like:

PS C:\Users\Dad\scripts> .\FindSymLinks.ps1 ..\Documents
    Directory: C:\Users\Dad\Documents
Mode               LastWriteTime     Length Name
----               -------------     ------ ----
d--hs       10/24/2009   2:09 PM           My Music
d--hs       10/24/2009   2:09 PM           My Pictures
d--hs       10/24/2009   2:09 PM           My Videos
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s