I know this is a rather old question, but since it's tagged PowerShell
and there hasn't been a PowerShell
specific response, here goes:
I wanted to get the value of the environment variable %SystemRoot%
on a remote machine, without enabling WinRM
.
My invocation in PowerShell
was:
$ret = & PsExec.exe \\RemoteMachine powershell.exe -Command '$env:SYSTEMROOT'
This returned an array of type Object[]
, with one element per line of output received.
Sample output, with corresponding array index:
PS> $ret
(0)
(1) PsExec v2.2 - Execute processes remotely
(2) Copyright (C) 2001-2016 Mark Russinovich
(3) Sysinternals - www.sysinternals.com
(4)
(5) C:\WINDOWS
As you can see my desired output was the 6th value in the returned array.
This does also work with PowerShell
scripts or commands that output more than one line, as each printed line is appended to the array as a new element.
With that in mind we can tidy our returned output up with the following:
$ret = $ret[5..($ret.Count - 1)]
This basically just removes the first five elements.
I have not tested this solution with programs other than PowerShell
though, so use with care.