How do run a powershell script as an admin?

I have the silent uninstall/wait/install script below that I need to push out to users, but I need to script it so it runs as administrator and I found some scripts, but I'm not understanding how to script it, any help is appreciated. Also, do I have to put the administrator script in twice? (i.e. in the first line of the uninstall script then before the line of the 2nd install script) or just once when I run it?

Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "On-Screen Takeoff"} | foreach-
object -process {$_.Uninstall()} 
Start-Sleep -Seconds 25
Start-Process "\\PATH_REMOVED\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi" $arguments

0 Comments   [ + ] Show comments

Answers (1)

Posted by: SMal.tmcc 7 years ago
Red Belt



  • did the below, but I get the error "Powershell.exe:Missing expression after unary operator '-': At line:1 char 15, any ideas?

    powershell.exe -elevated -scriptblock {
    Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "On-Screen Takeoff"} | foreach-object -process {$_.Uninstall()} Start-Sleep -Seconds 25 $arguments="/quiet" Start-Process "\\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi" $arguments } - sagunseanchetry 7 years ago
    • what vesion is powershell on the machine you are trying to run this on? 1.0 or 2.0 - SMal.tmcc 7 years ago
  • I'm running it on a windows powershell ISE and PowerGUI Script Editor v3.5 thank you. - sagunseanchetry 7 years ago
  • The script below generates an error, but for some reason it still works, any ideas on the error?

    start-process powershell -verb runas -argument
    Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "On-Screen Takeoff"} | foreach-object -process {$_.Uninstall()}
    Start-Sleep -Seconds 25
    Start-Process "\\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi" $arguments


    Start-Process : Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.String[]' and try again.
    At C:\Users\TEMP\AppData\Local\Temp\0f078c98-bbed-48be-966d-58e2f5f40e8c.ps1:1 char:47
    + start-process powershell -verb runas -argument <<<<
    + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.StartProcessCommand

    __GENUS : 2
    __RELPATH :
    __DERIVATION : {}
    __SERVER :
    __PATH :
    ReturnValue : 0 - sagunseanchetry 7 years ago
    • Update: if I run "Set-item wsman:localhost\client\trustedhosts -value RM-Client1" as an Admin in Powershell Console then close that then open Powershell in a standard user account it runs without error (the script below), but the only part of part2.ps1 it executes is the uninstall, not the install so another step closer

      i researched more and modified the script to run script below which calls script "part2.ps1" but the error stumps me. I can run the script if I am logged in as admin, but if I log in as a standard user and try to run the script it fails with that error. My goal is to use this script and deploy it non-admin users via GPO using Active Directory and that is why I need it to work. I found a link to a possible solution, but I don't really understand it, if you can shed any light, I'd appreciate it, thanks. (http://www.thecodeking.co.uk/2011/02/winrm-with-mixed-domain-environments.html)

      $username = '.\admin'
      $password = 'Window's''
      $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
      Invoke-Command -FilePath \\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\part2.ps1 -Credential $cred -ComputerName $env:computername

      PART 2
      Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "On-Screen Takeoff"} | foreach-object -process {$_.Uninstall()}
      Start-Sleep -Seconds 25
      Start-Process "\\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi" $arguments

      [j3q0yw1-e6530] Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication
      : There are currently no logon servers available to service the logon request.
      Possible causes are:
      -The user name or password specified are invalid.
      -Kerberos is used when no authentication method and no user name are specified.
      -Kerberos accepts domain user names, but not local user names.
      -The Service Principal Name (SPN) for the remote computer name and port does not exist.
      -The client and remote computers are in different domains and there is no trust between the two domains.
      After checking for the above issues, try the following:
      -Check the Event Viewer for events related to authentication.
      -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
      Note that computers in the TrustedHosts list might not be authenticated.
      -For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
      + CategoryInfo : OpenError: (:) [], PSRemotingTransportException
      + FullyQualifiedErrorId : PSSessionStateBroken - sagunseanchetry 7 years ago
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ