How to run an executable through PowerShell without batch files or cmd? - powershell

I am currently using batch files to run a set of simulations. Each line in the batch file reads:
"filepath\program.exe" "filepath\simulation.file"
The quotation marks exist to bound any spaces that exist within the file paths. Without any spaces in the file paths, the quotation marks can be removed.
When I run the batch file through PowerShell, using the following command, it works fine:
.\batch.bat
The executable is run and the output is written to the host, as if I was running the same batch file in cmd.
However, I want to ditch the batch files and run the command directly through PowerShell. When I run the following, I get the program to execute, though it doesn't run properly and I don't get anything written to host. It also appears to hang until I use Ctrl+C to cancel.
& "filepath\program.exe" "filepath\simulation.file"
Could you please help me with the following?
Any resources discussing how PowerShell executes batch files.
How to run an executable through PowerShell without using cmd or a batch file and have it write to host.

Have you tried Start-Process?
Start-Process -FilePath "filepath\program.exe" -ArgumentList "filepath\simulation.file" -Wait
PS: The Parameter -Wait is optional.

Although I'm not quite sure why yet, I found out that this issue only occurred while working remotely. Whenever I was connected to the network locally the command ran just fine.
Since I plan to execute the command on PCs that are situated locally on the network, I'll leave it at this for now.
Thanks to everyone who commented!

Related

How would I run a script where the .exe runs in one location and the script in another location?

After searching for the question, I didn't see anything outside of running multiple scripts in one file.
I'm trying to figure out how to run a script where the script lives in one directory and the .exe file that the script is running lives in another one.
If you're curious, the ask is coming from a person that stores hundreds of scripts in a separate location from any executable files. I was asked specifically to help with the script itself on this particular project. I'm not well versed in script writing, so I thought I would reach out here and see.
Below is the current code in the script that runs perfectly. The other parts of the script are specific to the program being installed and aren't relevant to actually starting the install.
I'm using Powershell to run this.
Start-Process -FilePath "$currentpath\ClientSetup_111.exe" -ArgumentList $InstCmdLine -Wait -PassThru

Run a VBScript using Start-Process

I am trying to run a VBScript using some code I already have written for generically running executables. I have used it successfully with EXE & BAT files, but VBS files are giving me headaches.
I found this that does it with CMD and Invoke-Expression, and this that does it with &. But I have found no references to doing it with Start-Process, and when I try something simple like
Start-Process wscript.exe -ArgumentList:"UNC PATH TO HelperTest.vbs"
I get
Execution of the Windows Script Host failed. (Not enough storage is
available to complete ths operation. )
But I can run the super simple VBS file directly no problem. This only happens when the VBS is on a network share. Running it locally works fine. Also, not including a fully qualified path works fine, as in
Start-Process wscript.exe -ArgumentList:"HelperTest.vbs"
So it's not an actual permissions issue on the share. Am I just bumping in to some obscure bug and I am not going to get this to work, or am I doing something wrong and getting confused by a not very helpful error message?

Execute Batch in Powershell (Win 10) does not affect Parent Shell

just for understanding this.
I want to open my Powershell in a certain folder. As I didn´t find out how, I tried to put a batch file with just "cd ....." in it in the default folder where PowerShell opens.
When I execute the batch, though, I end up where I started from.
It seems that the batch gets excuted in a subshell which doesn´t affect the Parentshell.
How can I execute the stuff in the batchfile in parentshell ?
Thanks in advance!
You cannot. Batch files are executed by cmd, not PowerShell, so there will always be a new process for them.
With a PowerShell script you can use dot-sourcing
. Script.ps1
To execute the script in your current scope, which is most similar to how batch files are executed by cmd by default.
If you want to open your Powershell in a certain folder, you can set that up in your Powershell profile. In Powershell, type $profile and that will give you the location of your profile file. Edit that file and use Set-Location:
Set-Location 'C:\Some\Place'
Powershell will execute whatever is in your profile script every time you open a new Powershell session.

Running a small WMI Powershell Script

I'm trying to have a few scripts that I can map to run from my keyboard for quickly changing the monitor/screen brightness. After some searching on the internet, I found this script which works when I enter it into Powershell.
$monitor=#(gwmi WmiMonitorBrightnessMethods -ns root/wmi)[0]
$monitor.WmiSetBrightness(50,0)
After I saved it as a .ps1 file and tried running it from the file, powershell tells me: The term "path of the file" is not recognized as the name of a cmdlet, function... and so on.
I'm not familiar with Powershell at all, can someone help with what I need to add in order for the script to run properly?
By default you can't run a PowerShell script that is in the current directory without putting .\ in front of the script name, or calling the full path of the script.
This is a security feature.
If you are in the directory that contains the script, run it by executing in a PowerShell window:
.\yourscript.ps1
Where yourscript is the name of your script.
See here for more information: https://ss64.com/ps/syntax-run.html
You may also see this error if your script has spaces in its name. If that is the case, enclose the path in quotes:
.\'your script.ps1'

Powershell.exe running the script in cli, or a wrapper?

I have a third-party application that's extensible by adding exe-files that perform dataconversion etc. I've written an extension in Powershell that does the conversion I want, but I'm unable to get the third-party app to run my ps1, as it will only accept an .exe file as an extension.
The app adds a filename as the first (and only) commandline argument to the extension, so the command it runs looks like:
someprogram.exe somefile.xml
I tried to get it to run Powershell.exe with my script as an argument, but I haven't been able to figure out how and if that's possible. Some stuff I tried like
powershell.exe myscript.ps1
won't work. I tried getting the script to find the correct XML file itself, but still somehow I couldn't get Powershell to run off the commandline and take a script as an argument and run it.
Next I thought about writing a small .exe file that only runs the Powershell script, but is that even possible? If it is, could someone nudge me in the right direction?
Powershell wants to have a qualified path to script files before it will run them. So you need to either use
powershell.exe .\myscript.ps1
if it lies in the current working directory (unlikely and prone to break for this use case) or use the full path to the script:
powershell.exe C:\Users\Foo\Scripts\myscript.ps1
or similar.
You can also try Powershell.exe -Command "& your-app.exe ${your-arguments}