ZTree.com  | ZEN  | About...  

 Index   Back

[Help!] Running elevated Powershell from F9 ?   [Help!]

By: Ben Kent       
Date: Oct 14,2019 at 09:39
In Response to: [Help!] Running elevated Powershell from F9 ? (Laurent Duchastel)

> > > Useless and intrusive Office Hub app keeps reinstalling itself.
> I
> > > want to easily uninstall it routinely with a F9 script.
> > >
> > > How do I run this elevated Powershell script from F9 ?
> > >
> > > Get-AppxPackage *officehub* | Remove-AppxPackage
> > >
> > > This below *doesn't* work:
> > >
> > >
> > > "%#ZTTOOLS%\elevate\elevate64" -c powershell.exe
> Get-AppxPackage
> > *officehub* | Remove-AppxPackage
> > >
> > >
> > > where elevate64 is a free utility to bypass UAC warning
> > > http://code.kliu.org/misc/elevate/
> > >
> > > Thank you for your hint.
> >
> > Try "%#ZTTOOLS%\elevate\elevate64" -c -w ...
> >
> > Hope this helps, let us know.
> >
> > Ron Metzger
>
>
> Solved the issue!
>
> It wasn't the -w parameter which did the trick, but added quotation
> marks to delimit the PS script
>
> "%#ZTTOOLS%\elevate\elevate64"
> -c powershell.exe "Get-AppxPackage *officehub* | Remove-AppxPackage"
> pause
>


> This leads to a question : what would be the best way to run with F9 a
> PS script that span many lines ?

1. separate your powershell commands with ; and put them in one line.
Difficult to read and it can be hard to get some commands to work that way.

2. Call an powershell script from the F9 entry
i.e. Create the .ps1 script before, and call it from the F9 menu item.
It will work, but not what you asked for.

3. Generate a temporary PS1 script, but need to watch out for characters that need escaping.

echo Get-AppxPackage *officehub* ^| Remove-AppxPackage >%#ZTTemp%\~Temp.ps1
echo Write-Output "Done." >>%#ZTTemp%\~Temp.ps1
"%#ZTTOOLS%\elevate\elevate64" -c -w powershell -file %#ZTTemp%\~Temp.ps1

You could use this syntax, with that you have to escape brackets, as shon
(
echo line^(1^)
echo line^(2^)
)>%#ZTTemp%\~Temp.ps1


4. Hybrid script
Taking inspiration from https://stackoverflow.com/questions/1947...ch-file-one-command-in-multiple-lines
powershell block comments can be used to create a hybrid CMD PS1 script


----------------
<# : Begin batch script
@echo off
REM The Powershell section needs two trailing empty lines
"%#ZTTOOLS%\elevate\elevate64" -c -w powershell -command - < %0
exit /b
----- Begin PS1 script -- #>
Get-AppxPackage *officehub* | Remove-AppxPackage


----------------


Ben

87 views      
 

Messages in this Thread

 
94,579 Postings in 11,930 Threads, 348 registered users, 58 users online (1 registered, 57 guests)
Index | Admin contact |   Forum Time: Nov 15, 2019 - 2:09 am EST  |  Hits:29,103,967  (1,181 Today )
RSS Feed