【PoweShell】Get-Comandでコマンドレットを絞り込む
結論から言うと2通りあります
ここではこれらを取得する方法とその構成を説明します
コマンドレットのContentに注目します
※PowerShellでは特に指定しない限りコマンド中の大文字小文字は区別されません
もっとも簡単な方法
コマンド
>gcm -noun content
- 短縮コマンドの説明
| 短縮名 | コマンドレット | 説明 |
| gcm | Get-Command | コマンドレットの情報を取得する |
結果
CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Add-Content 7.0.0.0 Microsoft.PowerShell.Management Cmdlet Clear-Content 7.0.0.0 Microsoft.PowerShell.Management Cmdlet Get-Content 7.0.0.0 Microsoft.PowerShell.Management Cmdlet Set-Content 7.0.0.0 Microsoft.PowerShell.Management
表示をスマートに
コマンド
>gcm -noun contnt | select name,version
結果
Name Version ---- ------- Add-Content 7.0.0.0 Clear-Content 7.0.0.0 Get-Content 7.0.0.0 Set-Content 7.0.0.0
他の方法
コマンド
>gcm | ?{$_.noun -eq "content"} | select name,version
結果
Name Version ---- ------- Add-Content 7.0.0.0 Clear-Content 7.0.0.0 Get-Content 7.0.0.0 Set-Content 7.0.0.0
コマンド
>gcm | ?{$_.name -like "*content"} | select name,version >gcm | ?{$_.commandtype -eq "cmdlet" -and $_.name -like "*content"} | select name,version
結果
Name Version ---- ------- Add-Content 7.0.0.0 Clear-Content 7.0.0.0 Get-Content 7.0.0.0 Get-WindowsImageContent 3.0 Set-Content 7.0.0.0
- 短縮コマンドの説明
| 短縮名 | コマンドレット | 説明 |
| ? | Where-Object | オブジェクトを列挙する |
| select | Select-Object | オブジェクトプロパティを選択する |
正式なコマンドレット名に展開すると上の2つのコマンドは次のようになります
>Get-Command | Where-Object{$PSItem.Noun -eq "Content"} | Select-Object Name,Version >Get-Command | Where-Object{$PSItem.Name -like "*Content"} | Select-Object name,version
フィルタに使えるメンバー名(表タイトル)
- 短縮コマンドの説明
| 短縮名 | コマンドレット | 説明 |
| gm | Get-Member | メンバーの情報を取得する |
コマンド
>gcm | gm
結果
TypeName: System.Management.Automation.AliasInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
Definition Property string Definition {get;}
Description Property string Description {get;set;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
Options Property System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTy…
Parameters Property System.Collections.Generic.Dictionary[string,System.Management.Automation.Parameter…
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Comm…
ReferencedCommand Property System.Management.Automation.CommandInfo ReferencedCommand {get;}
RemotingCapability Property System.Management.Automation.RemotingCapability RemotingCapability {get;}
ResolvedCommand Property System.Management.Automation.CommandInfo ResolvedCommand {get;}
Source Property string Source {get;}
Version Property version Version {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
DisplayName ScriptProperty System.Object DisplayName {get=if ($this.Name.IndexOf('-') -lt 0)…
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference…
ResolvedCommandName ScriptProperty System.Object ResolvedCommandName {get=$this.ResolvedCommand.Name;}
TypeName: System.Management.Automation.FunctionInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString Method string ToString()
CmdletBinding Property bool CmdletBinding {get;}
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property string DefaultParameterSet {get;}
Definition Property string Definition {get;}
Description Property string Description {get;set;}
HelpFile Property string HelpFile {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
Noun Property string Noun {get;}
Options Property System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTy…
Parameters Property System.Collections.Generic.Dictionary[string,System.Management.Automation.Parameter…
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Comm…
RemotingCapability Property System.Management.Automation.RemotingCapability RemotingCapability {get;}
ScriptBlock Property scriptblock ScriptBlock {get;}
Source Property string Source {get;}
Verb Property string Verb {get;}
Version Property version Version {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference…
TypeName: System.Management.Automation.CmdletInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property string DefaultParameterSet {get;}
Definition Property string Definition {get;}
HelpFile Property string HelpFile {get;}
ImplementingType Property type ImplementingType {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
Noun Property string Noun {get;}
Options Property System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTy…
Parameters Property System.Collections.Generic.Dictionary[string,System.Management.Automation.Parameter…
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Comm…
PSSnapIn Property System.Management.Automation.PSSnapInInfo PSSnapIn {get;}
RemotingCapability Property System.Management.Automation.RemotingCapability RemotingCapability {get;}
Source Property string Source {get;}
Verb Property string Verb {get;}
Version Property version Version {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
DLL ScriptProperty System.Object DLL {get=$this.ImplementingType.Assembly.Location;}
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference…上の方法では余分な情報があるのでCmdletInfoに絞り込みます
コマンド
>gcm|gm|?{$_.typename -eq "system.management.automation.cmdletinfo"} >gcm|gm|?{$_.typename -like "*cmdletinfo"}
結果
TypeName: System.Management.Automation.CmdletInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property string DefaultParameterSet {get;}
Definition Property string Definition {get;}
HelpFile Property string HelpFile {get;}
ImplementingType Property type ImplementingType {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
Noun Property string Noun {get;}
Options Property System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTy…
Parameters Property System.Collections.Generic.Dictionary[string,System.Management.Automation.Parameter…
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Comm…
PSSnapIn Property System.Management.Automation.PSSnapInInfo PSSnapIn {get;}
RemotingCapability Property System.Management.Automation.RemotingCapability RemotingCapability {get;}
Source Property string Source {get;}
Verb Property string Verb {get;}
Version Property version Version {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
DLL ScriptProperty System.Object DLL {get=$this.ImplementingType.Assembly.Location;}
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference…先のコマンドではDifinitionの一部が省略されて表示されてます
これらを折り返して表示するには次のようにします
コマンド
>gcm|gm|?{$_.typename -eq "system.management.automation.cmdletinfo"} | ft -wrap
- 短縮コマンドの説明
| 短縮名 | コマンドレット | 説明 |
| ft | Format-Table | 出力の列内の表示変更 |
結果
TypeName: System.Management.Automation.CmdletInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property string DefaultParameterSet {get;}
Definition Property string Definition {get;}
HelpFile Property string HelpFile {get;}
ImplementingType Property type ImplementingType {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
Noun Property string Noun {get;}
Options Property System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTyp
eName] OutputType {get;}
Parameters Property System.Collections.Generic.Dictionary[string,System.Management.Automation.ParameterM
etadata] Parameters {get;}
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Comma
ndParameterSetInfo] ParameterSets {get;}
PSSnapIn Property System.Management.Automation.PSSnapInInfo PSSnapIn {get;}
RemotingCapability Property System.Management.Automation.RemotingCapability RemotingCapability {get;}
Source Property string Source {get;}
Verb Property string Verb {get;}
Version Property version Version {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
DLL ScriptProperty System.Object DLL {get=$this.ImplementingType.Assembly.Location;}
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
try
{
[Microsoft.PowerShell.Commands.GetHelpCodeMethods]::GetHelpUri($this)
}
catch {}
finally
{
$ProgressPreference = $oldProgressPreference
};}
もっと詳しく
次のコマンドではGet-Contentのメンバー(タイトル)とその中身を表示します
コマンド
>gcm | ?{$_.name -eq "get-content"} | select *
結果
HelpUri : https://go.microsoft.com/fwlink/?LinkID=2096490
DLL : C:\Program Files\PowerShell\7\Microsoft.PowerShell.Commands.Management.dll
Verb : Get
Noun : Content
HelpFile : Microsoft.PowerShell.Commands.Management.dll-Help.xml
PSSnapIn :
Version : 7.0.0.0
ImplementingType : Microsoft.PowerShell.Commands.GetContentCommand
Definition :
Get-Content [-Path] <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>] [-Filter <s
tring>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-Delim
iter <string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonP
arameters>]
Get-Content -LiteralPath <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>] [-Filt
er <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-
Delimiter <string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<Co
mmonParameters>]
DefaultParameterSet : Path
OutputType : {System.Byte, System.String}
Options : ReadOnly
Name : Get-Content
CommandType : Cmdlet
Source : Microsoft.PowerShell.Management
Visibility : Public
ModuleName : Microsoft.PowerShell.Management
Module : Microsoft.PowerShell.Management
RemotingCapability : PowerShell
Parameters : {[ReadCount, System.Management.Automation.ParameterMetadata], [TotalCount, System.Management.Auto
mation.ParameterMetadata], [Tail, System.Management.Automation.ParameterMetadata], [Path, System.
Management.Automation.ParameterMetadata]…}
ParameterSets : {[-Path] <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>] [-Filter <string>] [-I
nclude <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-Delimiter <strin
g>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParameters>]
, -LiteralPath <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>] [-Filter <string
>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-Delimiter
<string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParame
ters>]}これらの情報を見るとコマンドレット名[Name]はメンバー[Verb(動詞)]とメンバー[Noun(名詞)]の組み合わせであることが判ります
従ってメンバー[Noun]でフィルタすると使用可能な動詞[Verb]の一覧が得られます
これで最初のフィルタするコマンドが理解できたと思います
このフィルタリング手法はGet-Commandの他にGet-ChildItem、Get-Service、Get-Processなどまたファンクションですがよく使うGet-NetIPAddressなどでも応用可能です
Contentについて
- 短縮コマンドの説明
| 説明 | ||
| cat | Get-Content | 指定された場所にあるアイテムのコンテンツを取得します 基本的にファイルの内容を取得します |
| gc | ||
| type | ||
| ac | Add-Content | ファイルに追記します |
| clc | Clear-Content | ファイルの内容をクリアします |
| sc | Set-Content | ファイルに新たに内容を書き込みます ※PowerShell7以降ではこの短縮名は使えません |
他にもいくつか注意点があります
次のサイトを参照してください
tech.guitarrapc.com
ファイル書き込み時にはOut-Fileを使うことも考慮してください
またファイル書き込みにはリダイレクト「>や>>」もあるので必要に応じて使い分けるのが良いでしょう
www.itlab51.com
重要追記
紹介した上記サイトに書かれている「デフォルトエンコーディングの違い」についてですがPowerShell7[pwsh]では仕様が変わったようです
結論から言うとpwshではOut-File,Set-Content,リダイレクト(>,>>)で全てASCIIとして出力されるようになったようです
PowerShell[powershell]でも確認しましたが、この場合はエンコーディングが違うことを確認しました
Set-ContentのみASCII
これらに注意してください