¿Cómo utilizar variables de entorno en una canalización de PowerShell?

¿Cómo utilizar variables de entorno en una canalización de PowerShell?

El comando

systeminfo | find "System Type"

se supone que regresara

System Type:               x64-based PC

tanto en una ventana del símbolo del sistema como en PowerShell.

En mi caso dice

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

porque como me explicaron enhttps://github.com/MicrosoftDocs/WSL/issues/1025, tengo otro findencygwinque está en mi camino.

Lo que funciona correctamente en este caso es

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

Ahora supongamos que quiero hacerlo independiente de dónde reside el sistema (digamos, si quiero incluir este comando en alguna guía para usuarios descuidados o similares). Pensé en invocar en su lugar

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

Esto funciona en un símbolo del sistema pero no en PowerShell. En este último he probado

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

pero esto da el error

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

También recibo un error si hago la ruta completa pero sin .exe: esto

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

da

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

yo también lo intenté

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

pero esto produce una avalancha de errores como

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

¿Qué está pasando? ¿Cómo hacerlo correctamente?

Respuesta1

Podrías hacerlo:

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

tenga en cuenta el signo comercial antes del comando.

Pero aún mejor, use PowerShell nativo en lugar de canalizar resultados que no sean de PowerShell a programas que no sean de PowerShell.

(Get-ComputerInfo -Property CsSystemType).CsSystemType

información relacionada