From: Ardis Lu Date: Mon, 27 May 2024 20:51:30 +0000 (-0700) Subject: Try to install to env:PSModulePath before using hardcoded path X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=36ead9dbfa2538ec080dab20a3353beae5b38b53;p=ArdisUtilities Try to install to env:PSModulePath before using hardcoded path --- diff --git a/README.md b/README.md index 7f19a97..ed522dd 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ To keep the module installed on future sessions, pass the `-Persist` parameter: "& { $(irm 'https://raw.githubusercontent.com/ardislu/ArdisUtilities/main/install.ps1') } -Persist" | iex ``` -To uninstall, delete the `ArdisUtilities` folder within `$HOME/Documents/PowerShell/Modules`. +To uninstall, delete the `ArdisUtilities` folder within the first location in `$env:PSModulePath` (typically `$HOME\Documents\PowerShell\Modules` (Windows) or `$HOME/.local/share/powershell/Modules` (non-Windows)). ### Manual diff --git a/install.ps1 b/install.ps1 index 4c03dcf..bac47dc 100644 --- a/install.ps1 +++ b/install.ps1 @@ -7,10 +7,14 @@ .DESCRIPTION By default, the ArdisUtilities module will be installed for the current session only. If '-Persist' is specified, then the ArdisUtilities module will be installed to - the "$HOME/Documents/PowerShell/Modules" folder. + the first location found in $env:PSModulePath. If no location is found in $env:PSModulePath, + ArdisUtilities will be installed to "$HOME\Documents\PowerShell\Modules" (Windows) or + "$HOME/.local/share/powershell/Modules" (non-Windows). .PARAMETER Persist - Install the ArdisUtilities module to "$HOME/Documents/PowerShell/Modules". + Install the ArdisUtilities module to the first location found in $env:PSModulePath, or + "$HOME\Documents\PowerShell\Modules" (Windows) or "$HOME/.local/share/powershell/Modules" (non-Windows) + if no location is found. .EXAMPLE PS> ./install.ps1 @@ -20,7 +24,7 @@ .EXAMPLE PS> ./install.ps1 -Persist - Installs ArdisUtilities to "$HOME/Documents/PowerShell/Modules", persisting the module across sessions. + Installs ArdisUtilities to the first location in $env:PSModulePath, persisting the module across sessions. .EXAMPLE PS> irm 'https://raw.githubusercontent.com/ardislu/ArdisUtilities/main/install.ps1' | iex @@ -30,7 +34,7 @@ .EXAMPLE PS> "& { $(irm 'https://raw.githubusercontent.com/ardislu/ArdisUtilities/main/install.ps1') } -Persist" | iex - Invoke this script from GitHub, installing ArdisUtilities to "$HOME/Documents/PowerShell/Modules" and + Invoke this script from GitHub, installing ArdisUtilities to the first location in $env:PSModulePath and persisting the module across sessions. #> [OutputType([System.Void])] @@ -57,7 +61,19 @@ process { Import-Module $module if ($Persist) { - $destination = "$HOME/Documents/PowerShell/Modules" # Default path for persistent PowerShell modules + $destination = ($env:PSModulePath -Split ';')[0] + + # If no folder is found from PSModulePath, use default location for the CurrentUser scope. See: + # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath + if (-Not (Test-Path -LiteralPath $destination)) { + if ($IsWindows) { + $destination = "$HOME/Documents/PowerShell/Modules" + } + else { + $destination = "$HOME/.local/share/powershell/Modules" + } + } + if (Test-Path -LiteralPath "$destination/ArdisUtilities") { Remove-Item "$destination/ArdisUtilities" -Force -Recurse }