Run single line as Admin in script

Welcome Forums General PowerShell Q&A Run single line as Admin in script

Viewing 2 reply threads
  • Author
    • #246849
      Topics: 5
      Replies: 9
      Points: 68
      Rank: Member


      Question two of the day! (Can you tell I’m facing a deadline?)

      So I have a script that can run without elevation, however there is one line I need to run as an admin.

      Is there a way I can do this without running the entire script (or Powershell ISE) as admin?

    • #246858
      Senior Moderator
      Topics: 10
      Replies: 161
      Points: 871
      Helping Hand
      Rank: Major Contributor

      Yes, you can use Start-Process as described in this StackOverflow discussion. Note that by default this uses the credentials of the user executing the script, so it won’t work for normal users. It either has to be executed by an administrator, or administrator credentials must be supplied with the -Credential parameter.

    • #246864
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      you can’t run only a line as admin, but you can execute it in a new process with elevated privilege using -Verb parameter.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.