PowerShellの関数である、Functionの一覧を表示する方法を以下に記します。
使用したPowerShellのバージョンは、以下の通りです。
PS C:\> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 63
以下の構文でFunctionの一覧を表示することができます。
Get-ChildItem -path function:
また、以下のように操作しても同様の結果となります。
Set-Location function: Get-ChildItem
元に戻すときには、Cドライブであれば、c:と入力しEnterキーを押せば戻ります。
以下のように操作すると、指定したFunctionのスクリプトを見ることができます。
PS C:\Users\Sakura> Get-Item function:clear-host CommandType Name Version Source ----------- ---- ------- ------ Function Clear-Host
PS C:\Users\Sakura> (Get-Item function:clear-host).Definition $space = New-Object System.Management.Automation.Host.BufferCell $space.Character = ' ' $space.ForegroundColor = $host.ui.rawui.ForegroundColor $space.BackgroundColor = $host.ui.rawui.BackgroundColor $rect = New-Object System.Management.Automation.Host.Rectangle $rect.Top = $rect.Bottom = $rect.Right = $rect.Left = -1 $origin = New-Object System.Management.Automation.Host.Coordinates $Host.UI.RawUI.CursorPosition = $origin $Host.UI.RawUI.SetBufferContents($rect, $space) # .Link # http://go.microsoft.com/fwlink/?LinkID=225747 # .ExternalHelp System.Management.Automation.dll-help.xml
PS C:\Users\Sakura> Get-Item Function:\prompt CommandType Name Version Source ----------- ---- ------- ------ Function prompt
PS C:\Users\Sakura> (Get-Item Function:\prompt).Definition "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml
以下のようにメッセージを引数として受け取り表示する関数を作成し確認してみます。
PS C:\Users\Sakura> Function SayHi($msg="") { Write-Output "Hi! $msg" } PS C:\Users\Sakura> SayHi Sakura Hi! Sakura PS C:\Users\Sakura> Get-ChildItem Function:\SayHi CommandType Name Version Source ----------- ---- ------- ------ Function SayHi PS C:\Users\Sakura> (Get-ChildItem Function:\SayHi).Definition param($msg="") Write-Output "Hi! $msg"
以上、PowerShellのFunction一覧の表示および、Functionのスクリプト内容を表示する方法でした。