[PowerShell]PSでLinuxの「history」コマンド

2025/02/13に公開

おさらい

機能 PowerShell Linux
過去実施したコマンド一覧 Get-Content (Get-PSReadlineOption).HistorySavePath history
ファイルを開く notepad (あるいみ) vi

毎回、「Get-Content (Get-PSReadlineOption).HistorySavePath」を打つのは面倒
→コマンド化する
→定義ファイル(*.ps1)に関数を定義する


(1)PowerShellの定義ファイル在処を確認する

PowerShellの定義ファイルは「$PROFILE」に格納されている
以下、echoで中身を除くと「Microsoft.PowerShell_profile.ps1」ファイルの在処がでてくる

echo $PROFILE

実行結果

PS C:\Users\hogehoge> echo $PROFILE
C:\Users\hogehoge\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

(2)PowerShellの定義ファイルを編集する

「notepad」コマンドで定義ファイルを開いて編集準備

notepad $PROFILE

・実行結果

PS C:\Users\hogehoge> notepad $PROFILE

(3)関数を定義する

・関数「full_history」を定義する
処理内容は(2)の

Get-Content (Get-PSReadlineOption).HistorySavePath

・定義ファイルに追記

■関数記載方法:

function [コマンド名(※任意文字列)]{
    // 処理
}

・「full_history」を実際に定義する

function full_history{
	Get-Content (Get-PSReadlineOption).HistorySavePath
}

PowerShellを再起動する

(4)実行

以降、full_historyと打ち込むだけで
Linuxでの「history」同様の結果を得られる

PS C:\Users\hogehoge> full_history
aws configure
aws configure --profile terraform
aws configure
pwd
man
~省略~
full_history
PS C:\Users\hogehoge>

おまけ

Get-Contentの引数はたくさんあるので以下で確認
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.5

Discussion