-1

How to create file that executes using powershell with administrator privilege?
As for now, I run these commands manually.

# Make sure you're running [Powershell] as an Administrator.
Get-Service ssh-agent | Set-Service -StartupType Manual

Start the service

Start-Service ssh-agent

This should return a status of Running

Get-Service ssh-agent

Now load your key files into ssh-agent

ssh-add "C:\Users\Tech Support.ssh\id_rsa2.pub"

Is there anyway to do? Please advice.

Velvet
  • 1,739

1 Answers1

1

In a manual/interactive context approach, self-elevation can be performed.

$AdminSID = New-Object -TypeName 'System.Security.Principal.SecurityIdentifier' -ArgumentList 'S-1-5-32-544'

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole($AdminSID)) { $MyScript = $MyInvocation.MyCommand.Path $MyPowerShellArguments = '-ExecutionPolicy', 'Bypass', '-NoProfile', '-File', """$MyScript""" Start-Process -FilePath 'powershell.exe' -ArgumentList $MyPowerShellArguments -Verb 'RunAs' exit $LASTEXITCODE }

Make sure you're running [Powershell] as an Administrator.

Get-Service ssh-agent | Set-Service -StartupType Manual

Start the service

Start-Service ssh-agent

This should return a status of Running

Get-Service ssh-agent

Now load your key files into ssh-agent

ssh-add "C:\Users\Tech Support.ssh\id_rsa2.pub"


Alternative Solution: Running a Script via the Context Menu ("Run as administrator")

For your need to run PowerShell scripts with administrator privileges, besides programmatic self-elevation, there's a very common and straightforward solution for users who prefer manual interaction: using the "Run as administrator" option available in the Windows context menu.

This is an excellent alternative for scenarios where you don't need full automation (like a scheduled script) but rather a simple and reliable way to ensure your script starts with the necessary permissions.

How to Use the "Run as administrator" Context Menu Option

  1. Create Your PowerShell Script: Save all the commands you want to run with administrator privileges in a file with a .ps1 extension (e.g., my_admin_script.ps1).

  2. Navigate to the Script: Open File Explorer and locate the .ps1 file you just created.

  3. Run as Administrator:

  • Right-click on the .ps1 file.

  • In the context menu that appears, select "Run as administrator".

Note: On Windows 11, if you don't see the option directly, you might need to click "Show more options" first to reveal it.

The script will execute in a new PowerShell window with elevated privileges. For many use cases where execution is occasional and interactive, the context menu option is a perfectly adequate and easy-to-implement solution.


UAC and Running Scripts with Administrator Privileges

UAC is a security feature that notifies you and asks for your permission when a program or script tries to make changes requiring elevated (administrator) privileges.

  • If UAC is enabled (which is the default and recommended for security), whenever you try to run a script with administrative privileges (whether via the "Run as administrator" context menu option or through a script that programmatically self-elevates), UAC will display a confirmation prompt. You'll need to click "Yes" (or provide administrator credentials) for the elevated execution to proceed.
  • If UAC is disabled (which isn't recommended for security reasons), the confirmation prompt won't appear. The script will attempt to run with elevated privileges directly. However, it's important to note that disabling UAC doesn't grant administrative privileges to a standard user; it merely removes the notification barrier.

So, for your script to perform actions requiring administrator rights (like starting services or manipulating SSH keys), it will need elevated privileges. UAC acts as the gatekeeper, ensuring you (or an authorized administrator) explicitly grant that permission when requested.


João Mac
  • 526