PowerShell, Start an app hidden in background, then kill that app - powershell

An app I want to push to various systems (Notepad++) doesn't generate it's configuration file until after it starts, so I'd like to start the application quickly hidden in the background so that the config.xml is generated (grabbing the apps PID as it is starting), then kill the app a second later. I thought I could do this as follows:
Start-Process -WindowStyle Hidden -FilePath "C:\Program Files\Notepad++\notepad++.exe
But this does not work and the app opens normally. How can I start this app hidden in the background? Also, how can I get the PID as it is starting so that I can kill it shortly afterwards?

I don't think you can start an app like notepad++ without a window, it's a GUI application. As for the other question, you are looking for the PassThru switch. Some cmdlets do not pass the output to the pipeline if this is not specified
So you can do something like this:
$process = Start-Process -FilePath 'C:\Program Files\Notepad++\notepad++.exe' -PassThru
Stop-Process -Id $process.Id

Related

PowerShell: Make an Installation Process Silent

I have a script that calls an application from Microsoft's Company Portal and automatically installs it. This launches Company Portal, potentially disrupting end user workflow. Is there any way to make this process run in the background? I tried to add an Argument List, but the process still launches Company portal. Any ideas? Thanks.
$OutputFile = "$env:WINDIR\TEMP\PythoncpInstall.log"
$Process = "companyportal:ApplicationId=e60a5520-dc39-4156-9223-825264cd5145"
$ProcessArgs = " /s -Wait -NoNewWindow"
##########ERROR LOGGING#####
Function Set-WriteToLog ($Write1)
{
Write-Host "$(Get-Date -format yyyy-MM-dd-hh-mm-ss)`t-`t$Write1"
}
#########START OF SCRIPT BODY#############
Start-Transcript -Path $OutputFile
start-process $Process -ArgumentList $ProcessArgs
sleep 10
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("^{i}")
Stop-Transcript
Without knowing more about the application and its installer, I'm not sure how if what I will suggest will work. Please share this information when you can.
What might help you is using Start-Process with -RedirectStandardInput and -WindowStyle.
-RedirectStandardInput will allow you to provide a text file that will be passed to standard input. This might let you provide input, depending on how the installer is implemented.
-WindowStyle would allow you to launch the process minimized or hidden.
Unfortunately, the way your code is currently written relies on SendKeys, which would in turn rely on the window being visible and focused.
Please let me know what installer you're using (if it's external), and I might be able to provide more help.

How to open file in windows 10 default app from powershell

I am trying to write a simple shell script in powershell that opens a file in a default windows app and monitors the process to wait until the process has exited to continue. At a high level I would like to open a .mp4 file to trim it in the default windows 10 app. When manually editing the file it opens in the photos app to trim, I would like to automate the part of opening the file in the app.
Currently I'm able to monitor the photos app process and wait for the process to close using the following code:
Start-Process shell:AppsFolder\Microsoft.Windows.Photos_8wekyb3d8bbwe!App
Start-Sleep -Seconds 2
$proc = Get-Process -Name Microsoft.Photos
$proc.WaitForExit()
However I would like to start the process and pass in a filepath to open, for example:
Start-Process shell:AppsFolder\Microsoft.Windows.Photos_8wekyb3d8bbwe!App "C:\Some\Path\To\File.mp4"
If I can get the file to simply open in the photos app I can simply click the edit button manually but ideally I would be able to open the file in the edit UI like this:
Then the script would wait for the photos process to close to continue on. Does anyone have any ideas on how we can open a file in a default windows 10 app in this way from powershehll? Or if this is even possible?
Thank you!
I think you should explore the -Wait option of the Start-Process cmdlet
Start-Process "C:\Some\Path\To\File.mp4" -Wait

How do I get a Powershell process that was opened by another Powershell process?

I am running multiple PowerShell scripts at once. I would like to be able to wait on certain ones to finish before opening new scripts. Basically, I was thinking if I could find the command line option that ran it something like "powershell.exe -Path "<script dir>" that would do it.
I tried doing a Get-Process | gm to find any parameters that I could call to get that information and I didn't see any (doesn't mean they aren't there) I tried looking through Task Manager to see if I could view something through the gui that I could link to but that didn't help either.
I hope I can get something like
Start-Process -FilePath ".\<script>.ps1" -ArgumentList "<args>"
do
{
sleep 10
}
until ((Get-Process -ProcessName "PowerShell" | where "<paramater>" -EQ ".\<script>")
I need to wait until that process is done but I don't want to put a wait at the end of the Start-Process because after that Start-Process kicks off I need some other items to go to while my .\ is running. I just need it to wait before another section of script kicks off.
Have a look at the "Job" cmdlets https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_jobs?view=powershell-6
And the $PID automatic variable, this will give the process ID of the current PowerShell session.

How can I launch this PowerShell script in a new hidden window

I am working on a Reboot Scheduling application to be used on simple remote machines. It has to be user friendly though so I have to have multiple scripts and tasks in the background. my main GUI script needs to launch a Secondary Script in a NEW hidden window so that the Main GUI Script can close.
the Secondary Script (Reboot.ps1) will run until the machine restarts if the user has a scheduled reboot pending.
The code below starts the Secondary Script hidden but as a sort of "child job", since this script can run "forever" it wont end thus leaving the Main GUI script frozen open until killed in task manager (not very user friendly I know..)
$program = Powershell.exe -windowstyle hidden -file "C:\Reboot.ps1"
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock("Invoke-Command {$program}")
Invoke-Command -NoNewScope -Scriptblock $scriptblock
So what I'm asking is if anyone knows how to start the Secondary Script in a new PowerShell window, instance, environment, anything that allows the Main GUI Script to close. Preferably, less intensive if possible. :) thank you!
Use Start-Process's own -WindowStyle Hidden parameter to launch your script hidden and asynchronously (Start-Process's default):
Start-Process -WindowStyle Hidden powershell.exe -Args '-File', 'C:\Reboot.ps1'
Your code looks really complicated for what you're trying to achieve, use Start-Process
Start-Process powershell.exe -ArgumentList '-windowstyle hidden -file "C:\Reboot.ps1"'

Kill a separate powershell script from the main script

I'm using a Powershell script to perform some automated testing on a web application.
Part of this script runs a small, separate script which basically monitors the web app for pop ups and closes them if they appear. It is called during the main script like so:
Start-Process Powershell.exe -Argumentlist "-file C:\Users\Documents\Monitor.ps1"
At some point though I would like to close the monitor script, perform some commands, and then start the monitor script again.
Is there a way for me to kill the monitor script from the main, without closing the main script as well in the process?
You would want to save it to a variable:
$a = start-process notepad.exe -PassThru
$a.Id
10536
So you could later kill it.