Add cd-users.ps1

This commit is contained in:
Markus Fleschutz 2022-09-25 10:13:38 +02:00
parent 34fbaec4b7
commit b1798e378b
2 changed files with 38 additions and 7 deletions

26
Scripts/cd-users.ps1 Normal file
View File

@ -0,0 +1,26 @@
<#
.SYNOPSIS
Sets the working directory to the users directory
.DESCRIPTION
This PowerShell script changes the working directory to the users directory.
.EXAMPLE
PS> ./cd-users
📂C:\Users
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
$Path = Resolve-Path "$HOME/.."
if (-not(Test-Path "$Path" -pathType container)) {
throw "Users directory at 📂$Path doesn't exist (yet)"
}
Set-Location "$Path"
"📂$Path"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

View File

@ -6,9 +6,9 @@
.EXAMPLE
PS> ./list-special-folders
Folder Name Folder Path
----------- -----------
Home Folder C:\Users\Markus
Folder Name Folder Path
----------- -----------
📂Autostart C:\Users\Markus\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
...
.LINK
https://github.com/fleschutz/PowerShell
@ -16,10 +16,6 @@
Author: Markus Fleschutz | License: CC0
#>
function AddLine { param([string]$FolderName, [string]$FolderPath)
New-Object PSObject -property @{ 'Folder Name' = "$FolderName"; 'Folder Path' = "$FolderPath" }
}
function GetTempDir {
if ("$env:TEMP" -ne "") { return "$env:TEMP" }
if ("$env:TMP" -ne "") { return "$env:TMP" }
@ -27,6 +23,10 @@ function GetTempDir {
return "C:\Temp"
}
function AddLine { param([string]$FolderName, [string]$FolderPath)
New-Object PSObject -property @{ 'Folder Name' = "$FolderName"; 'Folder Path' = "$FolderPath" }
}
function ListSpecialFolders {
if ($IsLinux) {
AddLine "📂Desktop" "$HOME/Desktop"
@ -38,6 +38,8 @@ function ListSpecialFolders {
AddLine "📂My Screenshots" "$HOME/Pictures/Screenshots"
AddLine "📂My Videos" "$HOME/Videos"
AddLine "📂Temporary Folder" "$(GetTempDir)"
$Path = Resolve-Path "$HOME/.."
AddLine "📂Users" "$Path"
} else {
$Path = Resolve-Path "$HOME/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup"
AddLine "📂Autostart" "$Path"
@ -51,6 +53,9 @@ function ListSpecialFolders {
AddLine "📂My Screenshots" "$([Environment]::GetFolderPath('MyPictures'))\Screenshots"
AddLine "📂My Videos" "$([Environment]::GetFolderPath('MyVideos'))"
AddLine "📂Temporary Folder" "$(GetTempDir)"
$Path = Resolve-Path "$HOME/.."
AddLine "📂Users" "$Path"
AddLine "📂Windows" "$([Environment]::GetFolderPath('Windows'))"
}
}