Last active
February 12, 2025 11:37
-
-
Save GorlikItsMe/3c350c6c56607e4ea57495c813061f20 to your computer and use it in GitHub Desktop.
My Powershell utils
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
| # How to install: | |
| # 1. open powershell | |
| # 2. `notepad $profile` | |
| # 3. Copy everything paste there and save | |
| # 4. restart terminal | |
| # https://stackoverflow.com/questions/72236557/how-do-i-read-a-env-file-from-a-ps1-script | |
| function LoadEnvFile { | |
| param ( | |
| [string]$EnvFile = ".env", | |
| [bool]$Verbose = $false | |
| ) | |
| if (Test-Path $envFile) { | |
| Get-Content $envFile | foreach { | |
| $line = $_.Trim() | |
| if (-not ([string]::IsNullOrWhiteSpace($line)) -and -not ($line.StartsWith("#"))) { | |
| $name, $value = $line -split '=', 2 | |
| if ($value -eq $null) { | |
| continue | |
| } | |
| # get first character of value | |
| $firstChar = $value.Substring(0, 1) | |
| $lastChar = $value.Substring($value.Length - 1, 1) | |
| # if value starts with " and ends with " then remove them | |
| if ($firstChar -eq '"' -and $lastChar -eq '"') { | |
| $value = $value.Substring(1, $value.Length - 2) | |
| } | |
| if ($firstChar -eq "'" -and $lastChar -eq "'") { | |
| $value = $value.Substring(1, $value.Length - 2) | |
| } | |
| if ($verbose) { | |
| Write-Host "$name=$value" | |
| } | |
| Set-Item -Path "env:\$name" -Value $value | |
| } | |
| } | |
| Write-Host "Environment variables loaded from $envFile" | |
| } else { | |
| Write-Warning "No .env file found" | |
| } | |
| } | |
| # Automaticly activate python venv if found and check if requirements.txt are up to date | |
| function ActivetePythonVenvIfExists { | |
| if (Test-Path ".venv") { | |
| Write-Host "Activating python venv..." | |
| $venvPath = Get-Location | |
| $venvPath = $venvPath.ToString() + "\" + ".venv\Scripts\Activate.ps1" | |
| & $venvPath | |
| # check if requirements.txt file exists | |
| if (-not (Test-Path "requirements.txt")) { | |
| Write-Warning "requirements.txt file not found. Cannot check if all required packages are installed" | |
| } else { | |
| # check if all required packages are installed | |
| $output = $(python -c "import pkg_resources; pkg_resources.require(open('requirements.txt',mode='r')); print('deps_installed')" 2>$null) | |
| if ($output -ne "deps_installed") { | |
| # [System.Char]::ConvertFromUtf32([System.Convert]::toInt32("26A0",16)) # ⚠️ | |
| Write-Warning "Your .venv is out of sync with requirements.txt" | |
| Write-Warning "run 'pip install -r requirements.txt' to install missing packages" | |
| # Write-Host "Installing required packages..." | |
| # $output = python -m pip install -r requirements.txt | |
| } | |
| } | |
| } | |
| } | |
| ActivetePythonVenvIfExists | |
| # ======================= Shortcuts =============================== | |
| function neofetch { | |
| winfetch | |
| } | |
| # Just open explorer where i am | |
| function Tutaj { | |
| explorer.exe . | |
| } | |
| # https://git-fork.com/ | |
| function Fork { | |
| & $env:LOCALAPPDATA\Fork\Fork.exe (Get-Location) | |
| } | |
| # open new terminal as admin. Its like sudo but without command (usefull for windows 10 where sudo dont exist) | |
| # function elevate_me { | |
| # $currentDirectory = Get-Location | |
| # $terminal = 'wt'; | |
| # $command = '"cd \"' + $currentDirectory + '\"; '+ $terminal +'"' | |
| # Start-Process -Verb RunAs -FilePath "powershell" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command $command" | |
| # } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment