Windows Powershell を5系から7系にアップデートする
概要
- PowerShell でのコマンド操作で
-Encoding UTF8
というオプションを使いたい場面がでてきた - 上記は5系だと使えないので、最新の7系にアップデートを行う
-Encoding UTF8
というオプションを使いたい場面がでてきた# masOSで自動生成される不可視ファイルを一括削除する
function Set-removeMacFiles {
# 現在のディレクトリを取得
$currentDirectory = Get-Location
# ログファイルのディレクトリを設定
$logDirectory = "C:\Users\hi3103\OneDrive\scripts\log\"
# ログファイル名を生成 (例: "Log_2023-10-02_12-30-00.txt")
$logFileName = "Log_" + (Get-Date -Format "yyyy-MM-dd_HH-mm-ss") + ".txt"
$logFilePath = Join-Path $logDirectory $logFileName
# .DS_Store および `._` から始まるファイルを再帰的に検索し、削除
Get-ChildItem -Path $currentDirectory -Recurse -Force -File | Where-Object {
$_.Name -eq ".DS_Store" -or $_.Name -like '._*'
} | ForEach-Object {
try {
# -LiteralPath を使用して、特殊文字を含むパスも正確に削除
Remove-Item -LiteralPath $_.FullName -Force -ErrorAction Stop
$message = "Deleted: " + $_.FullName
# コンソールに表示
Write-Host $message
# ログファイルに書き込み
Add-Content -Path $logFilePath -Value $message
} catch {
$errorMessage = "Failed to delete: " + $_.FullName
$errorDetail = $_.Exception.Message
# コンソールにエラー情報を表示
Write-Host $errorMessage -ForegroundColor Red
Write-Host $errorDetail -ForegroundColor Red
# ログファイルにエラー情報を書き込み
Add-Content -Path $logFilePath -Value $errorMessage
Add-Content -Path $logFilePath -Value $errorDetail
}
}
# 完了メッセージを表示
$completionMessage = "Deletion complete. Log File: $logFilePath"
Write-Host $completionMessage
Add-Content -Path $logFilePath -Value $completionMessage
}
New-Alias -Name removemacfiles -Value Set-removeMacFiles
C:\Users\ユーザー名\OneDrive\scripts\profile\removeMacFiles.ps1
上記フォルダに格納した .ps1 ファイルを、自動で $profile
にインポートするよう設定してある。
※参考:PowerShell スクリプトを OneDrive の中で管理する
removemacfiles
と入力して実行$profile
に、用意した保存場所のスクリプトをすべて読み込む設定を記述する.md
ファイルを毎週作成しているChatGPT-4 に以下の手順でやってもらった。
macOS の Terminal 上で使える open
コマンドを、Windows でも使えるようにしたい
open .
open ./hoge.html
open
コマンドは、PowerShell で考えると Invoke-Item
が該当しそうopen
と叩いたら、 Invoke-Item
が走るようにエイリアスを設定すればいいのでは?思いつきでChatGPT(GPT-4)にやり方を聞いたら簡単そうだったのでやってみた。