PowerShell/Scripts/list-special-folders.ps1

83 lines
2.7 KiB
PowerShell
Raw Normal View History

2022-09-19 14:15:58 +02:00
<#
.SYNOPSIS
Lists special folders
.DESCRIPTION
2022-12-28 14:47:29 +01:00
This PowerShell script lists all special folders (sorted alphabetically).
2022-09-19 14:15:58 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./list-special-folders.ps1
2022-09-19 14:15:58 +02:00
2022-09-25 10:13:38 +02:00
Folder Name Folder Path
----------- -----------
2022-09-25 10:58:46 +02:00
AdminTools 📂C:\Users\Markus\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
2022-09-19 14:15:58 +02:00
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
function GetTempDir {
if ("$env:TEMP" -ne "") { return "$env:TEMP" }
if ("$env:TMP" -ne "") { return "$env:TMP" }
2022-12-28 14:47:29 +01:00
if ($IsLinux) { return "/tmp/" }
2022-09-19 14:15:58 +02:00
return "C:\Temp"
}
2023-10-05 20:50:49 +02:00
function TryFolder([string]$name, [string]$path) {
if (Test-Path "$path" -pathType container) {
New-Object PSObject -property @{ 'Folder Name' = "$name"; 'Folder Path' = "📂$path" }
2022-12-28 14:47:29 +01:00
}
2022-09-25 10:13:38 +02:00
}
2022-09-19 14:15:58 +02:00
function ListSpecialFolders {
if ($IsLinux) {
2023-10-05 20:50:49 +02:00
TryFolder "Boot files" "/boot"
TryFolder "Config files" "/etc"
TryFolder "Desktop" "$HOME/Desktop"
TryFolder "Device files" "/dev"
TryFolder "Documents" "$HOME/Documents"
TryFolder "Downloads" "$HOME/Downloads"
TryFolder "Dropbox" "$HOME/Dropbox"
TryFolder "Home" "$HOME"
TryFolder "Mount points" "/mnt"
TryFolder "Music" "$HOME/Music"
TryFolder "Optional packages" "/opt"
TryFolder "Pictures" "$HOME/Pictures"
TryFolder "Recovered files" "/lost+found"
TryFolder "Removable media" "/media"
TryFolder "Repositories" "$HOME/Repos"
TryFolder "Repositories" "$HOME/Repositories"
TryFolder "Root" "/"
TryFolder "Screenshots" "$HOME/Pictures/Screenshots"
TryFolder "Snap" "$HOME/snap"
TryFolder "SSH" "$HOME/.ssh"
TryFolder "System binaries" "/sbin"
TryFolder "Trash" "$HOME/.local/share/Trash"
TryFolder "Templates" "$Home/Templates"
TryFolder "Temporary" "$(GetTempDir)"
2022-09-25 10:13:38 +02:00
$Path = Resolve-Path "$HOME/.."
2023-10-05 20:50:49 +02:00
TryFolder "Users" "$Path"
TryFolder "Videos" "$HOME/Videos"
2022-09-19 14:15:58 +02:00
} else {
2022-09-25 10:58:46 +02:00
$FolderNames = [System.Enum]::GetNames('System.Environment+SpecialFolder')
$FolderNames | Sort-Object | ForEach-Object {
if ($Path = [System.Environment]::GetFolderPath($_)) {
2023-10-05 20:50:49 +02:00
TryFolder "$_" "$Path"
2022-09-25 10:58:46 +02:00
}
}
2023-10-05 20:50:49 +02:00
TryFolder "Repositories" "$HOME\source\repos"
TryFolder "SSH" "$HOME\.ssh"
TryFolder "Temporary" "$(GetTempDir)"
2022-09-25 10:13:38 +02:00
$Path = Resolve-Path "$HOME/.."
2023-10-05 20:50:49 +02:00
TryFolder "Users" "$Path"
2022-09-19 14:15:58 +02:00
}
}
try {
2023-10-05 20:50:49 +02:00
ListSpecialFolders | Format-Table -property @{e='Folder Name';width=19},'Folder Path'
2022-09-19 14:15:58 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2022-12-28 14:47:29 +01:00
}