PowerShell/Scripts/list-special-folders.ps1

62 lines
1.8 KiB
PowerShell
Raw Normal View History

2022-09-19 14:15:58 +02:00
<#
.SYNOPSIS
Lists special folders
.DESCRIPTION
2022-09-25 10:58:46 +02:00
This PowerShell script lists the special folders (sorted alphabetically).
2022-09-19 14:15:58 +02:00
.EXAMPLE
PS> ./list-special-folders
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" }
if ($IsLinux) { return "/tmp" }
return "C:\Temp"
}
2022-09-25 10:13:38 +02:00
function AddLine { param([string]$FolderName, [string]$FolderPath)
2022-09-25 10:58:46 +02:00
New-Object PSObject -property @{ 'Folder Name' = "$FolderName"; 'Folder Path' = "📂$FolderPath" }
2022-09-25 10:13:38 +02:00
}
2022-09-19 14:15:58 +02:00
function ListSpecialFolders {
if ($IsLinux) {
2022-09-25 10:58:46 +02:00
AddLine "Desktop" "$HOME/Desktop"
AddLine "Downloads" "$HOME/Downloads"
AddLine "Home Folder" "$HOME"
AddLine "MyDocuments" "$HOME/Documents"
AddLine "MyMusic" "$HOME/Music"
AddLine "MyPictures" "$HOME/Pictures"
AddLine "MyScreenshots" "$HOME/Pictures/Screenshots"
AddLine "MyVideos" "$HOME/Videos"
AddLine "Temporary Folder" "$(GetTempDir)"
2022-09-25 10:13:38 +02:00
$Path = Resolve-Path "$HOME/.."
2022-09-25 10:58:46 +02:00
AddLine "Users" "$Path"
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($_)) {
AddLine "$_" "$Path"
}
}
AddLine "TemporaryFolder" "$(GetTempDir)"
2022-09-25 10:13:38 +02:00
$Path = Resolve-Path "$HOME/.."
2022-09-25 10:58:46 +02:00
AddLine "Users" "$Path"
2022-09-19 14:15:58 +02:00
}
}
try {
2022-09-25 10:58:46 +02:00
ListSpecialFolders | Format-Table -property @{e='Folder Name';width=22},'Folder Path'
2022-09-19 14:15:58 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}