PowerShell パイプラインで環境変数を使用するにはどうすればいいですか?

PowerShell パイプラインで環境変数を使用するにはどうすればいいですか?

コマンド

systeminfo | find "System Type"

戻ってくるはず

System Type:               x64-based PC

コマンド プロンプト ウィンドウと PowerShell の両方で実行できます。

私の場合は

/usr/bin/find: 'System Type': No such file or directory

なぜなら、私に説明されたようにhttps://github.com/MicrosoftDocs/WSL/issues/1025、私は別findシグウィンそれは私の道にあります。

この場合、正しく機能するのは

systeminfo | c:\windows\system32\find.exe "System Type"

さて、これをシステムの場所から独立させたいとします(例えば、このコマンドを不注意なユーザー向けのガイドなどに含めたい場合など)。代わりに以下を呼び出すことを考えました。

systeminfo | %SystemRoot%\system32\find.exe "System Type"

これはコマンドプロンプトでは機能しますが、PowerShellでは機能しません。後者では、

systeminfo | $env:SystemRoot\system32\find.exe "System Type"

しかし、これはエラーになります

Expressions are only allowed as the first element of a pipeline.

フルパスで次の行がない場合もエラーが発生します.exe

systeminfo | c:\windows\system32\find "System Type"

与える

Cannot run a document in the middle of a pipeline: C:\windows\system32\find

私も試してみました

systeminfo | % $env:SystemRoot\system32\find.exe "System Type"

しかし、これは次のようなエラーの雪崩を引き起こします

% : Input name "C:\WINDOWS\system32\find.exe" cannot be resolved to a method.

何が起こっているのでしょうか? どうすれば正しく実行できるのでしょうか?

答え1

次のようにすることができます:

systeminfo | & $env:SystemRoot\system32\find.exe "System Type"

コマンドの前のアンパサンドに注意してください。

しかし、PowerShell 以外の出力を PowerShell 以外のプログラムにパイプするのではなく、ネイティブ PowerShell を使用する方がさらに良いでしょう。

(Get-ComputerInfo -Property CsSystemType).CsSystemType

関連情報