ラベル PowerShell の投稿を表示しています。 すべての投稿を表示
ラベル PowerShell の投稿を表示しています。 すべての投稿を表示

2018年1月26日金曜日

エクスプローラーの何もないところを右クリック→そのパスをカレントディレクトリにコマンドプロンプト(cmd.exe)やPowerShellを起動

長いタイトルですが…。
本当はbashについても書きたかったのを泣く泣く削ってこの長さです。

何かというと、つまりこういうことをします。

デスクトップで右クリックすると、見慣れない

Open with Bash(B)
Open with cmd.exe(C)
Open with PowerShell(P)

というメニューが増えます。

以下画像参照。

こいつらをクリックすると、先程右クリックしたパス、
デスクトップをカレントディレクトリに、
bash、コマンドプロンプト、PowerShellが起動できます。

なんて楽なんでしょう。

エクスプローラー触っているときに「このパスで起動したいな」ってこと、
結構「あるある」じゃないでしょうか。


ということで、どうすれば追加できるか。

管理者権限でコマンドプロンプトを立ち上げて、以下を実行します。

reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithCmdexe /t REG_SZ /d "Open with cmd.exe(&C)"
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithCmdexe\command /t REG_SZ /d "cmd.exe"
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithPowershell /t REG_SZ /d "Open with Powershell(&S)"
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithPowershell\command /t REG_SZ /d "powershell.exe"
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithBash /t REG_SZ /d "Open with Bash(&B)"
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenWithBash\command /t REG_SZ /d "bash.exe"

以上。


WindowsのTipsを載せていきたいリポジトリ。

https://github.com/djeeno/windows/

2017/01/26 現在 空っぽに近い。

2018年1月18日木曜日

PowerShellスクリプト(.ps1)をバッチファイル(.bat)として実行したい

PowerShellスクリプト(.ps1)をバッチファイル(.bat)として実行したい。

そのような場合には、以下を1行目に追記する。

@setlocal enableextensions enabledelayedexpansion & set "PATH0=%~f0" & PowerShell.exe -Command "& (Invoke-Expression -Command ('{#' + ((Get-Content '!PATH0:'=''!') -join \"`n\") + '}'))" %* & exit /b !errorlevel!

以下、具体例。

@setlocal enableextensions enabledelayedexpansion & set "PATH0=%~f0" & PowerShell.exe -Command "& (Invoke-Expression -Command ('{#' + ((Get-Content '!PATH0:'=''!') -join \"`n\") + '}'))" %* & exit /b !errorlevel!

# 以下、PowerShellスクリプト
while (1) {
  Get-Date
  Start-Sleep 1
}

以上の内容をtest.bat等適当な名前で保存、実行すれば、
PowerShellをバッチファイルとして実行できます。


参考
https://www.pg-fl.jp/program/tips/ps1bat2.htm