PowerShellの基本操作②(ヘルプ参照方法や、コメント記号、複数行に分割する方法)

PowerShell

この記事では、PowerShellの基本操作②ということで、PowerShellのヘルプファイルの見方や、スクリプトを書く時のルールなどを説明します。

過去の記事は↓を参照してください。

参考
PowerShellの基本操作①
PowerShellの基本操作③

PowerShellのヘルプ参照方法

PowerShellでは、コマンドレット名の後ろに「-?」を付けて実行すると、ヘルプを表示できます。

Get-ChildItemのヘルプ表示例

PS> Get-ChildItem -?

名前
    Get-ChildItem

概要
    Gets the items and child items in one or more specified locations.


構文
    Get-ChildItem [[-Filter] <System.String>] [-Attributes {Archive | Compressed | Device | Directory | Encrypted | Hid
    den | IntegrityStream | Normal | NoScrubData | NotContentIndexed | Offline | ReadOnly | ReparsePoint | SparseFile |
     System | Temporary}] [-Depth <System.UInt32>] [-Directory] [-Exclude <System.String[]>] [-File] [-Force] [-Hidden]
     [-Include <System.String[]>] -LiteralPath <System.String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransact
    ion] [<CommonParameters>]

    :
   (省略)
    :

詳細なヘルプを表示したい場合は、「Get-Help」コマンドレットや「help」関数を使用します。

helpを使用した表示例

PS> help Get-ChildItem

名前
    Get-ChildItem

概要
    Gets the items and child items in one or more specified locations.


構文
    Get-ChildItem [[-Filter] <System.String>] [-Attributes {Archive | Compressed | Device | Directory | Encrypted | Hid
    den | IntegrityStream | Normal | NoScrubData | NotContentIndexed | Offline | ReadOnly | ReparsePoint | SparseFile |
     System | Temporary}] [-Depth <System.UInt32>] [-Directory] [-Exclude <System.String[]>] [-File] [-Force] [-Hidden]
     [-Include <System.String[]>] -LiteralPath <System.String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransact
    ion] [<CommonParameters>]

    Get-ChildItem [[-Path] <System.String[]>] [[-Filter] <System.String>] [-Attributes {Archive | Compressed | Device |
説明
    The `Get-ChildItem` cmdlet gets the items in one or more specified locations. If the item is a container, it gets t
    he items inside the container, known as child items. You can use the Recurse parameter to get items in all child co
    ntainers and use the Depth parameter to limit the number of levels to recurse.

    :
   (省略)
    :

「Get-Help」や「help」には、表示される情報量を調整するスイッチがあります。

スイッチ 説明
-detailed パラメーターの説明や例を含むコマンドレットの詳細なヘルプを表示する
-full コマンドレットおよびそのパラメーターの技術情報を含め、コマンドレットに関して利用可能なすべてのヘルプを表示する
-examples  ヘルプ ファイルの例だけを表示する
-online ヘルプ トピックのオンライン バージョンを表示する

「Get-Command」コマンドレットの詳細を調べるには、次のコマンドレットを指定します。

PS> Help Get-Command -Detailed

または

PS> Get-Help Get-Command -Detailed

コメント記号

PowerShellでは、シャープ(#)記号はコメントとして解釈されます。

PS> #この行はコメントです
PS> Get-ChildItem  #カレントディレクトリのファイル一覧を表示します


    ディレクトリ: C:\temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2019/09/29     22:50             48 test1.txt                                                                                                         
-a----        2023/01/17     19:23              3 test.txt                                                                                                               

 

複数行の分割

コマンドレットの指定を複数行に分けたい場合は、バッククォート(`)記号を使用します。

複数行に分割した場合、継続行として>>プロンプトが表示されます。

複数行に分けたコマンドレットを実行する場合は、バッククォート(`)を付けずに、「Enter」キーを押します。

PS> Get-ChildItem -Path c:\temp `
 -Filter *.txt


    ディレクトリ: C:\temp


Mode                 LastWriteTime         Length Name                                                                                                                   
----                 -------------         ------ ----                                                                                                                   
-a----        2019/09/29     22:50             48 test1.txt                                                                                                         
-a----        2023/01/17     19:23              3 test.txt                                                                                                               

※バッククォート(`)はWindowsの日本語キーボードの場合、 「Shift」 キーを押しながら「@」 キーを押します。

まとめ

今回はPowerShellのヘルプ参照方法や、コメント記号、複数行に分割する方法について説明しました。

次回も基本操作について説明します。

タイトルとURLをコピーしました