#winget installing program in new windows

1 messages · Page 1 of 1 (latest)

hollow herald
#

Trying to install some packages with winget inside a script but for some reason it just opens a new powershell window for each winget command which it doesnt if i just type it out myself
issue being it continues on with the rest of the script but like 2 seconds after that there is a restart command so it cant finish

try{
    winget install "7zip.7zip" --source "winget" --silent --accept-package-agreements --accept-source-agreements
    winget install "Foxit PDF Reader" --source "msstore" --silent --accept-package-agreements --accept-source-agreements
    winget install "Google.Chrome" --source "winget" --silent --accept-package-agreements --accept-source-agreements
    winget install "Oracle.JavaRuntimeEnvironment" --source "winget" --silent --accept-package-agreements --accept-source-agreements
}
#installeert via ninite als winget faalt
catch {
    Invoke-WebRequest -Uri "https://ninite.com/7zip-adoptjavax8-chrome-foxit/ninite.exe" -OutFile "H:\Temp\Ninite.exe"
    Start-Process "H:\Temp\Ninite.exe" -Wait
}
echo badger
hollow herald
echo badger
# hollow herald it does when its not installed which ive found to be the issue of it opening in ...

it just opens a new powershell window for each winget command which it doesnt if i just type it out myself
if you want to mimic typing winget --help then use either

& winget @('--help')
# or
start-process 'winget' -NoNewWindow -ArgumentList @('--help')

You're erroring is that winget isn't installed?
Here's how I'd handle it. when winget isn't installed, it exits on the very first line $binWget =

Or you could use -ea continue|ignore then throw your own exception.

function Invoke-WinGet {
    [CmdletBinding()]
    <#
    .synopsis
        call winget, when not installed: exits early throwing an error
    #>
    param(
       [object[]]$ArgsList
    )
    # If someone writes an alias or function named winget, normally it can
    # cause your code to call the function
    # I used 'Application' because that means a native command, like .exe or .cmd
    # filtering out any aliases or functions with the same name.
    $binWget = Get-Command -Name 'winget' -CommandType Application -ea stop
    'Invoking: winget {0}' -f @(
       $ArgsList -join ' '
    ) | Write-verbose

    & $binWget @ArgsList
}

Invoke-WinGet -Verbose -ArgsList @('--help')
# or
start 'winget' -ArgumentList @('list', '--help') -NoNewWindow
#

right before this it has install-module WingetTools
using $x = command causes it to wait for command to finish
start-process exits immediately unless you use -wait

#

and use -NoNewWindow otherwise they likely won't install in order, or in the same session