PowerShell/Docs/check-symlinks.md

100 lines
2.6 KiB
Markdown
Raw Normal View History

2023-07-29 10:15:44 +02:00
PS> *./check-symlinks.ps1*
2023-07-29 10:04:38 +02:00
====================
2021-11-08 21:36:42 +01:00
2022-11-17 19:46:02 +01:00
This PowerShell script checks every symbolic link in a folder (including subfolders).
It returns the number of broken symlinks as exit value.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./check-symlinks.ps1 [[-Folder] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2022-11-17 19:46:02 +01:00
-Folder <String>
Specifies the path to the folder
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2022-11-17 19:46:02 +01:00
PS> ./check-symlinks C:\Users
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.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
.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++
2023-05-26 12:20:18 +02:00
"Symlink $Symlink to: $Target seems broken (#$NumBroken)"
2022-11-17 20:02:26 +01:00
}
}
$NumTotal++
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
if ($NumTotal -eq 0) {
2023-05-26 12:20:18 +02:00
"✔️ found no symlink at 📂$FullPath in $Elapsed sec."
2022-11-17 20:02:26 +01:00
} elseif ($NumBroken -eq 1) {
2023-05-26 12:20:18 +02:00
"✔️ found $NumBroken broken symlink at 📂$FullPath in $Elapsed sec."
2022-11-17 20:02:26 +01:00
} else {
2023-05-26 12:20:18 +02:00
"✔️ found $NumBroken broken symlinks at 📂$FullPath in $Elapsed sec."
2022-11-17 20:02:26 +01:00
}
exit $NumBroken
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-07-29 10:17:33 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-symlinks.ps1 as of 07/29/2023 10:17:20)*