Last active
September 17, 2025 01:17
-
-
Save hymkor/1b4db95531f5a294ea7f9f214e95ed7f to your computer and use it in GitHub Desktop.
指定された実行ファイルを、環境変数PATHで指定されたディレクトリの同名ファイルに上書きコピーする PowerShell スクリプトです
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
| Set-PSDebug -Strict | |
| function Update-Exe($source){ | |
| if ( -not (Test-Path $source) ){ | |
| Write-Host "$source not found" | |
| Exit 1 | |
| } | |
| $name = (Split-Path $source -Leaf) | |
| Get-Command $name -All -ErrorAction SilentlyContinue | ForEach-Object { | |
| $target = $_.Source | |
| $dir = (Split-Path -Parent $target) | |
| $answer = (Read-Host "Update `"${target}`" ? [y|n]") | |
| if ( $answer -ieq "y" ){ | |
| Try{ | |
| Copy-Item $source -Destination $dir -PassThru -ErrorAction Stop | |
| } | |
| Catch{ | |
| $backup = (Join-Path $dir ($name + "-" + (Get-Random))) | |
| Move-Item $target $backup -PassThru | |
| Copy-Item $source -Destination $dir -PassThru | |
| } | |
| } | |
| } | |
| } | |
| if ( $args.Length -lt 1 ){ | |
| Get-ChildItem -Path . | ForEach-Object { | |
| if ( $_ -like "*.exe" -or $_ -like "*.ps1" -or | |
| $_ -like "*.cmd" -or $_ -like "*.bat" ){ | |
| Update-Exe $_ | |
| } | |
| } | |
| Exit 0 | |
| } | |
| foreach ( $source in $args ){ | |
| Update-Exe $source | |
| } | |
| #gist https://gist.github.com/hymkor/1b4db95531f5a294ea7f9f214e95ed7f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment