Created
November 2, 2024 01:17
-
-
Save SVNKoch/138bcdef27b53bc9af8a547cc523b768 to your computer and use it in GitHub Desktop.
Send To Context Menu Entry: Add Path to User PATH Environment Variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Add entry to windows file context menu which allows adding path of file(s) and folder(s) to user environment PATH variable. | |
| # Place shortcut in windows sendto location by pressing WIN + R and enter shell:sendto | |
| # Shortcut Details: | |
| # Destiantion: ` C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -File "C:\path to folder\addToPATH.ps1" ` | |
| # Execute in: ` "C:\path to folder" ` | |
| # Alternative Destination if using some silencer: ` C:\Windows\System32\wscript.exe "C:\path2 to folder\run_silent.vbs" "C:\path to folder\addToPATH.ps1" %* ` | |
| Add-Type -AssemblyName System.Windows.Forms | |
| function main { | |
| param ( | |
| [string[]]$PathsInSelection | |
| ) | |
| $global:pathsToAdd = @() | |
| $global:pathsAlreadyInPath = @() | |
| if (-not $PathsInSelection) { | |
| [System.Windows.Forms.MessageBox]::Show("No path(s) provided", "Paths to Add", [System.Windows.Forms.MessageBoxButtons]::OK) | |
| return | |
| } | |
| collectPathEntriesFromSelection -PathsInSelection $PathsInSelection | |
| displayConfirmationDialog | |
| } | |
| function collectPathEntriesFromSelection { | |
| param ( | |
| [string[]]$PathsInSelection | |
| ) | |
| foreach ($item in $PathsInSelection) { | |
| Write-Output "--> Handling: $item" | |
| if (Test-Path $item -PathType Leaf) { | |
| handleFile -filePath $item | |
| } elseif (Test-Path $item -PathType Container) { | |
| handleDirectory -directoryPath $item | |
| } else { | |
| handleError -item $item | |
| } | |
| } | |
| } | |
| function handleFile { | |
| param ([string]$filePath) | |
| Write-Output "Type: file" | |
| $directory = (Get-Item $filePath).DirectoryName | |
| Write-Output "Handle parent directory : $directory" | |
| collectPath -newPath $directory | |
| } | |
| function handleDirectory { | |
| param ([string]$directoryPath) | |
| Write-Output "Type: directory" | |
| $directory = (Get-Item $directoryPath).FullName | |
| collectPath -newPath $directory | |
| } | |
| function handleError { | |
| param ([string]$item) | |
| Write-Output "Type: not a valid file or directory" | |
| } | |
| function collectPath { | |
| param ([string]$newPath) | |
| if (($global:pathsToAdd -notcontains $newPath) -and ($global:pathsAlreadyInPath -notcontains $newPath)) { | |
| if (-not (pathExists -pathToCheck $newPath)) { | |
| Write-Output "Collection type: add to PATH" | |
| $global:pathsToAdd += $newPath | |
| } else { | |
| Write-Output "Collection type: already in PATH" | |
| $global:pathsAlreadyInPath += $newPath | |
| } | |
| } else { | |
| Write-Output "Collection type: already collected" | |
| } | |
| } | |
| function pathExists { | |
| param ([string]$pathToCheck) | |
| $userPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User) -split ";" | |
| $systemPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::Machine) -split ";" | |
| return ($userPath -contains $pathToCheck) -or ($systemPath -contains $pathToCheck) | |
| } | |
| function Wrap-Text { | |
| param ( | |
| [string]$text, | |
| [int]$width = 40 | |
| ) | |
| $wrapped = "" | |
| for ($i = 0; $i -lt $text.Length; $i += $width) { | |
| $wrapped += $text.Substring($i, [Math]::Min($width, $text.Length - $i)) + "`n " | |
| } | |
| return $wrapped.TrimEnd(" `n") | |
| } | |
| function displayConfirmationDialog { | |
| if ($global:pathsToAdd.Count -gt 0 -or $global:pathsAlreadyInPath.Count -gt 0) { | |
| Write-Output "" | |
| $message = "" | |
| Write-Output ("Count of Items that will get added to PATH: {0}" -f $global:pathsToAdd.Count) | |
| if ($global:pathsToAdd.Count -gt 0) { | |
| $message = "Add the following to PATH?`n`n" + (($global:pathsToAdd | Sort-Object | ForEach-Object { "- " + (Wrap-Text -text $_) }) -join "`n`n") | |
| } | |
| Write-Output ("Count of Items already in PATH: {0}" -f $global:pathsAlreadyInPath.Count) | |
| if ($global:pathsAlreadyInPath.Count -gt 0) { | |
| if ($message -ne "") { $message += "`n`n`n" } # Add spacing if both lists are present | |
| $message += "The following are already in PATH:`n`n" + (($global:pathsAlreadyInPath | Sort-Object | ForEach-Object { "- " + (Wrap-Text -text $_) }) -join "`n`n") | |
| } | |
| $result = [System.Windows.Forms.MessageBox]::Show($message, "PATH Change Confirmation", [System.Windows.Forms.MessageBoxButtons]::OKCancel) | |
| Write-Output "" | |
| if ($result -eq [System.Windows.Forms.DialogResult]::OK) { | |
| Write-Output "Decision: Confirmed adding path(s) to user PATH" | |
| addPathsToPATH | |
| } else { | |
| Write-Output "Decision: Cancel adding path(s) to user PATH" | |
| } | |
| } | |
| } | |
| function addPathsToPATH { | |
| Write-Output "" | |
| foreach ($path in $global:pathsToAdd) { | |
| $userPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User) -split ";" | |
| $updatedPath = ($userPath + $path) -join ";" | |
| [System.Environment]::SetEnvironmentVariable("PATH", $updatedPath, [System.EnvironmentVariableTarget]::User) | |
| Write-Output "Adding to user PATH: $path" | |
| } | |
| } | |
| main -PathsInSelection $args |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for silent running, you could use: https://gist.github.com/SVNKoch/93e272ccaff1bff19f429d7bf3c51725