.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
.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
.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])]
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
}