PowerShell/Docs/list-special-folders.md

93 lines
2.7 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *list-special-folders.ps1* Script
2022-11-17 20:02:26 +01:00
list-special-folders.ps1
2022-11-17 19:46:02 +01:00
## Parameters
```powershell
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists special folders
.DESCRIPTION
2023-05-26 12:20:18 +02:00
This PowerShell script lists all special folders (sorted alphabetically).
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./list-special-folders
Folder Name Folder Path
----------- -----------
AdminTools 📂C:\Users\Markus\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
...
.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" }
2023-05-26 12:20:18 +02:00
if ($IsLinux) { return "/tmp/" }
2022-11-17 20:02:26 +01:00
return "C:\Temp"
}
function AddLine { param([string]$FolderName, [string]$FolderPath)
2023-05-26 12:20:18 +02:00
if (Test-Path "$FolderPath" -pathType container) {
New-Object PSObject -property @{ 'Folder Name' = "$FolderName"; 'Folder Path' = "📂$FolderPath" }
}
2022-11-17 20:02:26 +01:00
}
function ListSpecialFolders {
if ($IsLinux) {
2023-05-26 12:20:18 +02:00
AddLine "Desktop" "$HOME/Desktop/"
AddLine "Documents" "$HOME/Documents/"
AddLine "Downloads" "$HOME/Downloads/"
AddLine "Dropbox" "$HOME/Dropbox/"
AddLine "Home" "$HOME/"
AddLine "Music" "$HOME/Music/"
AddLine "Pictures" "$HOME/Pictures/"
AddLine "Repositories" "$HOME/Repos/"
AddLine "Repositories" "$HOME/Repositories/"
AddLine "Screenshots" "$HOME/Pictures/Screenshots/"
AddLine "Snap" "$HOME/snap/"
AddLine "SSH" "$HOME/.ssh/"
AddLine "Trash" "$HOME/.local/share/Trash/"
AddLine "Templates" "$Home/Templates/"
AddLine "Temporary" "$(GetTempDir)"
2022-11-17 20:02:26 +01:00
$Path = Resolve-Path "$HOME/.."
2023-05-26 12:20:18 +02:00
AddLine "Users" "$Path/"
AddLine "Videos" "$HOME/Videos/"
2022-11-17 20:02:26 +01:00
} else {
$FolderNames = [System.Enum]::GetNames('System.Environment+SpecialFolder')
$FolderNames | Sort-Object | ForEach-Object {
if ($Path = [System.Environment]::GetFolderPath($_)) {
AddLine "$_" "$Path"
}
}
2023-05-26 12:20:18 +02:00
AddLine "Repositories" "$HOME\source\repos"
AddLine "SSH" "$HOME\.ssh"
AddLine "Temporary" "$(GetTempDir)"
2022-11-17 20:02:26 +01:00
$Path = Resolve-Path "$HOME/.."
AddLine "Users" "$Path"
}
}
try {
2023-05-26 12:20:18 +02:00
ListSpecialFolders | Format-Table -property @{e='Folder Name';width=18},'Folder Path'
2022-11-17 20:02:26 +01:00
exit 0 # success
} 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
2022-11-17 19:46:02 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of list-special-folders.ps1*