重定向可用于命令行的 Powershell 输出

Redirect Powershell output available to command line

我想知道如何使 powershell 变量可用以便从命令行读取。这可能吗?

命令是

get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" | Select -Expand macaddress -Last 1 | set-variable -name mac1

DOS 中没有 Powershell,所以我猜你有一个 CMD 脚本(即 .bat 或 .cmd 文件)需要分配一个从 Powershell 返回的变量。这是suprisingly quite tricky.

C:\>for /f "delims=" %i in ('powershell -command " & {  get-wmiobject win32_networkadapter -filter 'netconnectionstatus
= 2' | Select -Expand macaddress -Last 1 } "') do set foobar=%i

C:\>set foobar=00:19:99:E1:98:32

C:\>echo %foobar%
00:19:99:E1:98:32