Skip to content

Instantly share code, notes, and snippets.

@thakyZ
Created June 23, 2022 15:46
Show Gist options
  • Save thakyZ/935e23f862d17408dd41cf9a9cef3dd2 to your computer and use it in GitHub Desktop.
Save thakyZ/935e23f862d17408dd41cf9a9cef3dd2 to your computer and use it in GitHub Desktop.
An old tool to rebuild the bin folder directories (very buggy)
param(
[Parameter(Position=0,Mandatory=$false,HelpMessage="Enable output to console.")]
[switch]
$Output,
[Parameter(Position=1,Mandatory=$false,HelpMessage="Do current user only.")]
[switch]
$CurrentUser
)
$Root = "%APROG_DIR%\"
$GlobalPrograms = @(
"C:\Perl64\bin"
"C:\Python310"
"C:\Python310\Scripts"
"adb"
"bfg"
"bin"
"Caffeine"
"CMake\bin"
"colortool"
"composer"
"cygwin64"
"D\dmd2\windows\bin"
"D1\dmd\windows\bin"
"Git\bin"
"Git\cmd"
"depot_tools"
"depotdownloader"
"DMC\dm\bin"
"dnSpy"
"elm-format"
"emacs\bin"
"ExportTool"
"fciv"
"ffmpeg\bin"
"FileZilla FTP Client"
"gifski\win"
"gnuwin32\bin"
"Go\bin"
"gsfw"
"haste"
"iperf"
"JProfiler\bin"
"KiTTY"
"nmap"
"Pear"
"Phing"
"php-cs-fixer"
"php"
"phpcbf"
"phpcs"
"pngcrush"
"PuTTY"
"r\R-3.5.1patched\bin"
"resize"
"ruby25\Ruby25-x64\bin"
"shellcheck"
"terraform"
"tidy\bin"
"TortoiseSVN\bin"
"uncrusify"
"ungit"
"VisualD"
"whois"
"xmllint\bin"
"C:\Program Files\qemu"
"C:\Qt\5.15.2\msvc2019_64\bin"
"C:\Qt\5.14.2\msvc2017_64\bin"
"D:\Progra~2\Gpg4win\bin"
"D:\Progra~1\Guiffy"
"C:\Progra~1\Java\jre1.8.0_321\bin"
"C:\Progra~2\Common Files\Oracle\Java\javapath"
"C:\Progra~1\Microsoft\jdk-11.0.12.7-hotspot\bin"
"C:\Progra~1\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin"
"C:\Progra~1\NVIDIA GPU Computing Toolkit\CUDA\v11.5\libnvvp"
"C:\Progra~1\NVIDIA GPU Computing Toolkit\CUDA\v11.5\bin"
"D:\Progra~1\Guiffy"
"C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps"
"C:\Progra~1\Razer\ChromaBroadcast\bin"
"C:\Progra~2\NVIDIA Corporation\PhysX\Common"
"C:\Progra~1\NVIDIA Corporation\NVIDIA NvDLISR"
"C:\Progra~1\NVIDIA Corporation\Nsight Compute 2021.3.0\"
"C:\Progra~1\MariaDB\MariaDB Connector C 64-bit\lib\"
"C:\Progra~1\MariaDB\MariaDB Connector C 64-bit\lib\plugin\"
"C:\ProgramData\ComposerSetup\bin"
"D:\Progra~1\Gpg4win\..\GnuPG\bin"
"D:\Progra~2\Gpg4win\bin_64"
"D:\Progra~2\Gpg4win\bin"
"D:\Progra~2\GnuPG\bin"
"C:\Perl64\bin"
"C:\Progra~1\dotnet\"
"C:\Progra~1\nodejs\"
"D:\ProgramData\Anaconda3"
"D:\ProgramData\Anaconda3\Library\mingw-w64\bin"
"D:\ProgramData\Anaconda3\Library\usr\bin"
"D:\ProgramData\Anaconda3\Library\bin"
"D:\ProgramData\Anaconda3\Scripts"
"C:\Progra~1\Docker\Docker\resources\bin"
"C:\ProgramData\DockerDesktop\version-bin"
"C:\ProgramData\chocolatey\bin"
)
$UserPrograms = @(
"%USERPROFILE%\scoop\shims"
"%LOCALAPPDATA%\Microsoft\WindowsApps"
"%APPDATA%\npm"
"%APPDATA%\Python\Python310\site-packages"
"%APPDATA%\Python\Python310\Scripts"
"%USERPROFILE%\scoop\apps\oh-my-posh\current\bin"
"%USERPROFILE%\.cargo\bin"
"%USERPROFILE%\scoop\shims"
"%LOCALAPPDATA%\Programs\Microsoft VS Code\bin"
"%USERPROFILE%\.dotnet\tools"
"%USERPROFILE%\go\bin"
"%USERPROFILE%\Documents\PowerShell\Scripts"
)
$Equivelents = @{
"^C:\\Users\\$env:UserName" = "%USERPROFILE%";
"^C:\\Users\\$env:UserName\\AppData\\Roaming" = "%APPDATA%";
"^C:\\Users\\$env:UserName\\AppData\\Local" = "%LOCALAPPDATA%";
"^D:\\Files\\System\\Programs" = "%APROG_DIR%"
}
$TempPath = "";
$LocalPath = $null;
$MachinePath = $null;
if ($CurrentUser) {
$RegKeyU = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true)
$LocalPath = $RegKeyU.GetValue('path', $null, "DoNotExpandEnvironmentNames")
} else {
$RegKeyLM = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', $true)
$MachinePath = $RegKeyLM.GetValue('path', $null, "DoNotExpandEnvironmentNames")
}
function RemoveLocalMachine() {
$MachinePathArray = $MachinePath.Split(";");
$MachinePathArray = [System.Collections.ArrayList]$MachinePathArray;
ForEach ($Program in $GlobalPrograms) {
for ($k = 0; $k -le $MachinePathArray.Count; $k++) {
for ($i = 0; $i -le $Equivelents.Keys.Count; $i++) {
for ($j = 0; $j -le $Equivelents.Values.Count; $j++) {
$Key = $Equivelents.Keys[$i];
$Value = $Equivelents.Values[$j];
if ($MachinePathArray[$k] -match "${Key}" -and $Program -match "${Value}") {
$MachinePathArray[$k] = $MachinePathArray[$k] -Replace $Key, $Value;
}
if ($MachinePathArray[$k] -match "${Value}" -and $Program -match "${Value}") {
$MachinePathArray.Remove($MachinePathArray[$k])
}
}
}
}
}
ForEach ($Program in $GlobalPrograms) {
for ($k = 0; $k -le $MachinePathArray.Count; $k++) {
if ($MachinePathArray[$k] -eq $Program) {
$MachinePathArray.Remove($MachinePathArray[$k]);
}
if ($MachinePathArray[$k] -eq $Program) {
$MachinePathArray.Remove($MachinePathArray[$k]);
}
}
}
return ($MachinePathArray -join ";");
}
function DoLocalMachine() {
$MachinePath = RemoveLocalMachine;
ForEach($Program in $GlobalPrograms) {
if ("${Program}".Contains("C:\") -or "${Program}".Contains("D:\") -or "${Program}".Contains("E:\")) {
$ProgramPath = "${Program}";
} else {
$ProgramPath = "${Root}${Program}";
}
if ($TempPath -eq "") {
$TempPath = "${ProgramPath}";
} else {
$TempPath = "${TempPath};${ProgramPath}";
}
if ($Output) {
Write-Host "Adding Path: [${ProgramPath}]";
}
}
return $TempPath
}
function UpdateLMPath() {
$MachinePath = RemoveLocalMachine;
$MachinePathArrayTwo = ([System.Collections.ArrayList]($MachinePath.Split(";")))
$MachinePathArrayTwo.Remove("%APROG_LIST%");
$MachinePath = ($MachinePathArrayTwo -join ";");
if ($MachinePath[$MachinePath.Length - 1] -ne ";") {
$MachinePath = "${MachinePath};"
}
return "$MachinePath`%APROG_LIST`%;"
}
function RemoveCurrentUser() {
$LocalPathArray = $LocalPath.Split(";");
$LocalPathArray = [System.Collections.ArrayList]$LocalPathArray;
ForEach ($Program in $UserPrograms) {
for ($k = 0; $k -le $LocalPathArray.Split(" ").Length; $k++) {
if ($LocalPathArray[$k] -eq $Program) {
$LocalPathArray.Remove($LocalPathArray[$k]);
}
if ($LocalPathArray[$k] -eq $Program) {
$LocalPathArray.Remove($LocalPathArray[$k]);
}
for ($i = 0; $i -le $Equivelents.Keys.Split(" ").Length; $i++) {
for ($j = 0; $j -le $Equivelents.Values.Split(" ").Length; $j++) {
$Key = $Equivelents.Keys[$i];
$Value = $Equivelents.Values[$j];
if (($LocalPathArray[$k] -contains "${Key}" -and $Program -contains "${Value}") -or ($LocalPathArray[$k] -contains "${Value}" -and $Program -contains "${Key}")) {
$LocalPathArray.Remove($LocalPathArray[$k]);
}
}
}
}
}
return ($MachinePathArray -join ";");
}
function UpdateCUPath() {
$LocalPath = RemoveCurrentUser;
ForEach($Program in $UserPrograms) {
$ProgramPath = "${Program}";
if ($TempPath -eq "") {
$TempPath = "${ProgramPath}";
} else {
$TempPath = "${TempPath};${ProgramPath}";
}
}
$LocalPath = "${TempPath}${LocalPath}"
if ($LocalPath[$LocalPath.Length - 1] -ne ";") {
$LocalPath = "${LocalPath};"
}
return $LocalPath
}
$UpdatedPath = $null;
if ($CurrentUser) {
$UpdatedPath = UpdateCUPath;
Echo $UpdatedPath
#$RegKeyU.SetValue("path", "${UpdatedPath}", "ExpandString")
} else {
DoLocalMachine | Out-Null
$UpdatedPath = UpdateLMPath;
Echo $UpdatedPath
#$RegKeyLM.SetValue("APROG_LIST", ${(DoLocalMachine)}, "ExpandString")
#$RegKeyLM.SetValue("path", "${UpdatedPath}", "ExpandString")
}
if ($Output) {
Write-Host "Path added:"
$UpdatedPath.Split(";").ForEach({
Write-Host -Object "${_}"
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment