PowerShell: Redirect output of a command line tool to the Host - powershell

I am trying to invoke a command-line .exe tool (x264 to convert some videos) and print its output in PowerShell host.
There are a lot of parameters that need to be passed to the x264. So far I couldn't run it with the Invoke-Item cmdlet so I tried
[diagnostics.process]::start($com, $args).WaitForExit()
This works fine but it opens the old cmd window to show the output. Just out of curiosity I was wondering how can I show the output in the host.

I might be completely off, no PowerShell guru, but can't you just run the following?
$args = #("list", "of", "args", "here")
& x264.exe $args

When the cmd window opens, does it show the output? If so, maybe this can help.
Start-Process c:\x264.exe -ArgumentList $args -Wait -NoNewWindow

Related

Is there a way to open a CMD window with a running command through Powershell?

I know it is possible to use a batch file, but due to a suggestion I switched a lot of my script over to PowerShell. The problem I ran into is that PowerShell is still missing some commands from CMD and has lower permissions when run as an Admin. Below is the current line I have been using.
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList 'del /s "C:\Users\*.mp3"'"
I ran it in both 5.1 and 7.
It works with a simple command like opening the calculator or sending a ping, but I can't get the del command to work. The goal is to open a CMD window which will then delete all mp3 files. I know there are probably better ways to do this, but I more so want to know if it's possible than efficient. Thank you for your time!
This works somewhat
Start-Process cmd "/c del /S C:\Users\*.mp3"

Run a PowerShell method from cmd and don't wait for its return

In cmd I'm trying to run Method1 which is in a PowerShell script, script1.
Method1 is a method that takes a few hours, and I simply want to fire and forget.
The following is working for me:
c:\temp> powershell
PS c:\temp> . .\script1.ps1;Method1
When I do the lines above, everything is working fine as long as I keep the CMD of PS opened. once I close the PS window, it kills Method1.
So actually I want that from cmd, in one line, to somehow make Method1 work without the dependency of the PowerShell window, maybe create a new process.. I am not really sure.
I've tried:
c:\temp> cmd /c powershell . .\script1.ps1;Method1
It is running for a few seconds, but when the cmd gets closed, then Method1 also terminates.
I also tried
c:\temp>cmd /c powershell -noexit "& { . .\script.ps1;Method1 }"
Again, once I do this, it is working. However, a PowerShell window is opened and if I close it then it terminates Method1.
From you help, I've tried:
c:\temp> cmd /c powershell start-process cmd /c powershell . .\script1.ps1;Method1
But I get an exception:
Start-Process : A positional parameter cannot be found that accepts
argument 'powershell'.
But still, I am not able to make it work.
Alternatively if you want a pure PowerShell solution (note this needs to be running as Admin):
Invoke-Command LocalHost -Scriptblock $script -InDisconnectedSession
The InDisconnectedSession switch runs it in a separate PowerShell session that will not be terminated when you close the PowerShell window. You can also use Get-PSSession and pass the session to Enter-PSSession to interact with it during or after execution. Remember in this state if you close the window it -will- kill it, so you'll want to use Exit-PSSession to keep it alive.
There is however a problem - you can't do any remoting tasks, at least not easily. This incurs the wrath of the "double hop" where you remote to one computer (your own in this case), then to another, and for security PowerShell refuses to pass any credentials to the second remoting session so it can't connect, even if you put the credentials in manually. If you do need to do remoting I recommend sticking with launching a hidden PowerShell process.
You can use PowerShell jobs for that, so just:
Start-Job -FilePath somepath
And add a method call at the end of the script, or pass in a Scriptblock like:
Start-Job -ScriptBlock {. .\path_to_ps1; Method1}
Or perhaps use the hackish:
start-process cmd -WindowStyle Hidden -ArgumentList "'/c powershell . .\script1.ps1;Method1'"
Actually, you can just launch PowerShell, without CMD, and I am not sure why I was using a cmd approach:
start-process powershell -WindowStyle Hidden -ArgumentList ". .\script1.ps1;Method1"
Easy answer ya'll; Just paste "start" command into your PS window (whether in a remote session or not) and it works fine:
Start C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file 'driverletter:\path\yourpowershellscript.ps1'

How to get Powershell result from inner start-processs

I have a ps script like:
Start-Process powershell -Verb runAs -ArgumentList "Get-WebApplication"
Because Get-WebApplication needs admin right, so I use "-verb runAs".
My question is: now the result only shows in the new powershell windows. How can I make my main powershell get/display the result?
You have two choices that I see:
Run your script as administrator and just run Get-WebApplication directly.
Capture the output from the powershell process you're starting with Start-process and then do whatever you want with the output in your script. To capture the output you can either redirect the output to a file and then read the file in your script, or you can use .NET [diagnostics.process]::start to redirect stdout and directly read the output stream in your script (no file is created).

How to open Powershell Console Window from Powershell

I am writing a script to use multiple plink (PuTTY) sessions as a Windows version of clusterssh. I am stuck however because I want to open multiple Powershell windows from powershell. When I type the command for powershell, it opens a new session. This is similar to typing bash in bash. I want multiple physical windows opening.
I tried -windowstyle as well as the other args to no avail. I was wondering if there is a way you know of. I really appreciate your help. I looked and didn't find anything already here. Thanks for your time.
This will open a new window.
Either:
start-process powershell
Or:
start powershell
if you are trying to open a new window and launch a new script:
start powershell {.\scriptInNewPSWindow.ps1}
This will do it:
Invoke-Item C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
This works for me:
$argList = "-file `"$Location\script.ps1`""
Start-Process powershell -argumentlist $argList
(The backticks are necessary. This can be copied outright.) Variables can be used in the "-file" parameter (such as one set at the beginning of the script to reflect the location of the file) and spaces can appear in the variable due to the backticks.
Edited to use a two-line solution (the "$argList" variable) because PowerShell can mangle things otherwise.
To start Powershell 6 from a PS console start pwsh might do the trick.
It starts in the same folder.
(I haven't delved into it but I guess PS6's pwsh.exe has to be in the path for it to work.)

PowerShell and executable

Every time I run an application (.exe) in PowerShell, instead of executing it as it does with scripts, the program gets run in Command Prompt.
Invoke-Item MyProgram.exe
I thought PowerShell was supposed to replace the Command Prompt but instead PowerShell opens Command Prompt which then runs the program. Is there some way I can get PowerShell to completely replace the Command Prompt?
I thought PowerShell was supposed to replace the Command Prompt
It is, so why don't you just do MyProgram.exe
Your question is pretty vague, and in short, it will depend on the program because Invoke-Item uses the Windows default file association stuff by default.
I'm betting that using & MyProgram.exe instead of Invoke-Item MyProgram.exe will work though.
Did you try Start-Process Program.exe -NoNewWindow
I got the answer from another question:
Start-Process .\MyProgram.exe -NoNewWindow -Wait