次の方法で共有


Select-Xml

XML 文字列またはドキュメント内のテキストを検索します。

構文

Select-Xml
      [-Xml] <XmlNode[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      [-Path] <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -LiteralPath <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -Content <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]

説明

Select-Xml コマンドレットを使用すると、XPath クエリを使用して XML 文字列とドキュメント内のテキストを検索できます。 XPath クエリを入力し、ContentPath、または Xml パラメーターを使用して、検索する XML を指定します。

例 1: AliasProperty ノードの選択

PS C:\> $Path = "$Pshome\Types.ps1xml"
PS C:\> $XPath = "/Types/Type/Members/AliasProperty"
PS C:\> Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

Name                 ReferencedMemberName
----                 --------------------
Count                Length
Name                 Key
Name                 ServiceName
RequiredServices     ServicesDependedOn
ProcessName          Name
Handles              Handlecount
VM                   VirtualSize
WS                   WorkingSetSize
Name                 ProcessName
Handles              Handlecount
VM                   VirtualMemorySize
WS                   WorkingSet
PM                   PagedMemorySize
NPM                  NonpagedSystemMemorySize
Name                 __Class
Namespace            ModuleName

この例では、Types.ps1xml のエイリアス プロパティを取得します。 (このファイルの詳細については、about_Types.ps1xml を参照してください。

最初のコマンドは、Types.ps1xml ファイルへのパスを $Path 変数に保存します。

2 番目のコマンドは、AliasProperty ノードへの XML パスを$XPath変数に保存します。

3 番目のコマンドは、Select-Xml コマンドレットを使用して、Types.ps1xml ファイルから XPath ステートメントによって識別される AliasProperty ノードを取得します。 このコマンドでは、パイプライン演算子を使用して、AliasProperty ノードを Select-Object コマンドレットに送信します。 ExpandProperty パラメーターは、Node オブジェクトを展開し、その Name プロパティと ReferencedMemberName プロパティを返します。

結果には、Types.ps1xml ファイル内の各エイリアス プロパティの Name と ReferencedMemberName が表示されます。 たとえば、Length プロパティのエイリアスである Count プロパティがあります。

例 2: XML ドキュメントを入力する

PS C:\> [xml]$Types = Get-Content $Pshome\Types.ps1xml
PS C:\> Select-Xml -Xml $Types -XPath "//MethodName"

この例では、XML パラメーターを使用して、Select-Xml コマンドレットに XML ドキュメントを指定する方法を示します。

最初のコマンドでは、Get-Content コマンドレットを使用して Types.ps1xml ファイルの内容を取得し、$Types変数に保存します。 [xml] は、変数を XML オブジェクトとしてキャストします。

2 番目のコマンドでは、Select-Xml コマンドレットを使用して、Types.ps1xml ファイル内の MethodName ノードを取得します。 このコマンドでは、Xml パラメーターを使用して、$Types変数の XML コンテンツを指定し、XPath パラメーターを使用して MethodName ノードへのパスを指定します。

例 3: PowerShell ヘルプ ファイルを検索する

PS C:\> $Namespace = @{command = "https://schemas.microsoft.com/maml/dev/command/2004/10"; maml = "https://schemas.microsoft.com/maml/2004/10"; dev = "https://schemas.microsoft.com/maml/dev/2004/10"}

The second command saves the path to the help files in the $Path variable.If there are no help files in this path on your computer, use the Update-Help cmdlet to download the help files. For more information about Updatable Help, see about_Updatable_Help (https://go.microsoft.com/fwlink/?LinkId=235801).
PS C:\> $Path = "$Pshome\en-us\*dll-Help.xml"

The third command uses the **Select-Xml** cmdlet to search the XML for cmdlet names by finding Command:Name element anywhere in the files. It saves the results in the $Xml variable.**Select-Xml** returns a **SelectXmlInfo** object that has a Node property, which is a **System.Xml.XmlElement** object. The Node property has an InnerXML property, which contains the actual XML that is retrieved.
PS C:\> $Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"

The fourth command sends the XML in the $Xml variable to the Format-Table cmdlet. The **Format-Table** command uses a calculated property to get the Node.InnerXML property of each object in the $Xml variable, trim the white space before and after the text, and display it in the table, along with the path to the source file.
PS C:\> $Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize

Name                    Path
----                    ----
Export-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-Counter             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-WinEvent            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Import-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Add-Computer            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Add-Content             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Checkpoint-Computer     C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
...

この例では、Select-Xml コマンドレットを使用して、PowerShell XML ベースのコマンドレット ヘルプ ファイルを検索する方法を示します。 この例では、各ヘルプ ファイルのタイトルとして機能するコマンドレット名と、ヘルプ ファイルへのパスを検索します。

最初のコマンドは、ヘルプ ファイルに使用される XML 名前空間を表すハッシュ テーブルを作成し、$Namespace変数に保存します。

例 4: XML を入力するさまざまな方法

PS C:\> $Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>
        <edition language="Polish">Pl.Book1.com</edition>
      </editions>
    </project>
  </projects>
</Book>
"@

The second command uses the *Content* parameter of **Select-Xml** to specify the XML in the $Xml variable.
PS C:\> Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

The third command is equivalent to the second. It uses a pipeline operator (|) to send the XML in the $Xml variable to the **Select-Xml** cmdlet.
PS C:\> $Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

この例では、Select-Xml コマンドレットに XML を送信する 2 つの異なる方法を示します。

最初のコマンドは、xml を含む here 文字列を$Xml変数に保存します。 (here 文字列の詳細については、about_Quoting_Rulesを参照してください)。

例 5: 既定の xmlns 名前空間を使用する

PS C:\> $SnippetNamespace = @{snip = "https://schemas.microsoft.com/PowerShell/Snippets"}

The second command uses the **Select-Xml** cmdlet to get the content of the Title element of each snippet. It uses the *Path* parameter to specify the Snippets directory and the *Namespace* parameter to specify the namespace in the $SnippetNamespace variable. The value of the *XPath* parameter is the "snip" namespace key, a colon (:), and the name of the Title element.The command uses a pipeline operator (|) to send each **Node** property that **Select-Xml** returns to the ForEach-Object cmdlet, which gets the title in the value of the **InnerXml** property of the node.
PS C:\> Select-Xml -Path $Home\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" | foreach {$_.Node.Innerxml}

この例では、既定の xmlns 名前空間を使用する XML ドキュメントで Select-Xml コマンドレットを使用する方法を示します。 この例では、Windows PowerShell ISE ユーザーが作成したスニペット ファイルのタイトルを取得します。 スニペットの詳細については、「New-IseSnippet」を参照してください。

最初のコマンドは、スニペット XML ファイルが使用する既定の名前空間のハッシュ テーブルを作成し、$SnippetNamespace変数に割り当てます。 ハッシュ テーブルの値は、スニペット XML の XMLNS スキーマ URI です。 ハッシュ テーブルのキー名 snip は任意です。 予約されていない任意の名前を使用できますが、xmlns を使用することはできません。

パラメーター

-Content

検索する XML を含む文字列を指定します。 文字列をパイプして、Select-Xmlを することもできます。

型:String[]
配置:Named
規定値:None
必須:True
パイプライン入力を受け取る:True
ワイルドカード文字を受け取る:False

-LiteralPath

検索する XML ファイルのパスとファイル名を指定します。 Pathとは異なり、LiteralPath パラメーターの値は、型指定されたとおりに使用されます。 ワイルドカードとして解釈される文字はありません。 パスにエスケープ文字が含まれている場合は、単一引用符で囲みます。 単一引用符は、エスケープ シーケンスとして文字を解釈しないように PowerShell に指示します。

型:String[]
Aliases:PSPath
配置:Named
規定値:None
必須:True
パイプライン入力を受け取る:True
ワイルドカード文字を受け取る:False

-Namespace

XML で使用される名前空間のハッシュ テーブルを指定します。 @{<namespaceName> = <namespaceValue>} の形式を使用します。

XML が xmlns で始まる既定の名前空間を使用する場合は、名前空間名に任意のキーを使用します。 xmlns を使用することはできません。 XPath ステートメントで、各ノード名の前に名前空間名とコロン (//namespaceName:Node など) を付けます。

型:Hashtable
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-Path

検索する XML ファイルのパスとファイル名を指定します。 ワイルドカード文字を使用できます。

型:String[]
配置:1
規定値:None
必須:True
パイプライン入力を受け取る:True
ワイルドカード文字を受け取る:True

-Xml

1 つ以上の XML ノードを指定します。

XML ドキュメントは、XML ノードのコレクションとして処理されます。 XML ドキュメントをパイプ処理して Select-Xmlを すると、パイプラインを介して各ドキュメント ノードが個別に検索されます。

型:XmlNode[]
Aliases:Node
配置:1
規定値:None
必須:True
パイプライン入力を受け取る:True
ワイルドカード文字を受け取る:False

-XPath

XPath 検索クエリを指定します。 クエリ言語では大文字と小文字が区別されます。 このパラメーターは必須です。

型:String
配置:0
規定値:None
必須:True
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

入力

System.String or System.Xml.XmlNode

パスまたは XML ノードをこのコマンドレットにパイプできます。

出力

SelectXmlInfo

メモ

  • XPath は、XML ドキュメントの一部を識別するように設計された標準言語です。 XPath 言語の詳細については、XPath リファレンス と、MSDN ライブラリの イベント選択 の [選択フィルター] セクションを参照してください。