About non-interactive -d, it just runs the command, without returning output and then terminates remote prompt. Actually, it does, by only returning the error code of remote process. In your case, it shows the output of $file on a non-existent, hidden cmd window on remote machine and terminates. That's why you don't see anything. Try to create a file on remote machine using -d switch and then you see it works.
Update:
If $file is a variable in remote machine, the problem is you're using a variable that expands in your local powershell environment (as you mentioned) so use a cmd variable instead (considering you're sending the command to cmd) that doesn't expand in powershell. PSExec.exe \\$hostname -u user -p pass -d cmd /c "type %file%" 2> $null
However, that's not the REAL solution! because what if you want to send a command to powershell remotely? You need something preventing the expansion, like %%varibale%% in cmd. I've found using " will expand, but ' prevents expanding in powershell:
PS> "This is $file" against PS> 'This is $file'
But that doesn't help in your case, another way though, is to create an script in local machine, and send to destination with PSEXEC:
PSExec.exe \\$hostname -u user -p pass -c powershell script.ps1 2> $null
Or... use Invoke-Command as mentioned here:
PS C:\> invoke-command -filepath c:\scripts\test.ps1 -computerName $hostname
This does nothing, remove the cmd part and type it in your own command prompt, that's not an actual command. This is why it's returning nothing.
– Cole Jul 22 '16 at 19:14Start-Jobmy started job never returns. – sirdank Jul 22 '16 at 19:19C:\PSExec.exe \\$hostname /accepteula -u user -p pass -d cmdmeans you are runningcmdon remote system. tryC:\PSExec.exe \\$hostname -u user -p pass powershell– NetwOrchestration Jul 22 '16 at 19:26type C:\Shared\file.txtworks through the command prompt though. Powershell resolves$filebefore sending the command to PSExec. This line works great as long as I do it without-dand not inside of aStart-Jobtask. – sirdank Jul 22 '16 at 19:29