]> git.ardis.lu - ArdisUtilities/commitdiff
Try to install to env:PSModulePath before using hardcoded path
authorArdis Lu <ardisglu@gmail.com>
Mon, 27 May 2024 20:51:30 +0000 (13:51 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 27 May 2024 20:51:30 +0000 (13:51 -0700)
README.md
install.ps1

index 7f19a97c8bff967c2b45f25834b2b8fd6f1b3ffa..ed522dd76bc3dc55db9ac8cc9f7ae7eddfb646d3 100644 (file)
--- 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
 
index 4c03dcf528090d4363b79e97868309bb115a75d6..bac47dc35b8fabad34f2b6c45661eb181d186e0e 100644 (file)
@@ -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
     }
This page took 0.042905 seconds and 4 git commands to generate.