-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
check-symlinks.ps1
executable file
·55 lines (50 loc) · 1.68 KB
/
check-symlinks.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<#
.SYNOPSIS
Checks symlinks in a folder
.DESCRIPTION
This PowerShell script checks every symbolic link in a folder (including subfolders).
It returns the number of broken symlinks as exit value.
.PARAMETER folder
Specifies the path to the folder
.EXAMPLE
PS> ./check-symlinks C:\Users
⏳ Checking symlinks at 📂C:\Users including subfolders...
✅ Found 0 broken symlinks at 📂C:\Users in 60 sec
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Folder = "")
try {
if ($Folder -eq "" ) { $Folder = read-host "Enter the path to the folder" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$FullPath = Resolve-Path "$Folder"
"⏳ Checking symlinks at 📂$FullPath including subfolders..."
[int]$NumTotal = [int]$NumBroken = 0
Get-ChildItem $FullPath -recurse | Where { $_.Attributes -match "ReparsePoint" } | ForEach-Object {
$Symlink = $_.FullName
$Target = ($_ | Select-Object -ExpandProperty Target -ErrorAction Ignore)
if ($Target) {
$path = $_.FullName + "\..\" + ($_ | Select-Object -ExpandProperty Target)
$item = Get-Item $path -ErrorAction Ignore
if (!$item) {
$NumBroken++
"Symlink $Symlink to: $Target seems broken (#$NumBroken)"
}
}
$NumTotal++
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
if ($NumTotal -eq 0) {
"✅ No symlink found at 📂$FullPath in $Elapsed sec"
} elseif ($NumBroken -eq 1) {
"✅ Found $NumBroken broken symlink at 📂$FullPath in $Elapsed sec"
} else {
"✅ Found $NumBroken broken symlinks at 📂$FullPath in $Elapsed sec"
}
exit $NumBroken
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}