How to Run PowerShell Script as Administrator by Default?

PowerShell is an incredibly powerful tool for managing and automating tasks in Windows environments. It’s a common Administrator’s pitfall – forgetting to run a PowerShell script using the “Run as Administrator” option. Failing so could lead to many *weird* issues while running PowerShell scripts. E.g., In SharePoint, we get the “The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.” error.

Running a PowerShell script as an administrator is often necessary when executing scripts that require elevated privileges to modify system settings, install software, or manage resources. This article will guide you through running a PowerShell script as an administrator.

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered

Run PowerShell as Administrator

You need to run PowerShell as an administrator to install software, modify registry keys, or manage system settings. Without administrative privileges, PowerShell may not be able to execute certain commands, and you may receive errors or access denied messages.

So, How to run PowerShell as an administrator? The Solution is simple! To open Windows PowerShell as admin, Type “PowerShell” in the start menu >> Choose “Run as Administrator”. You can also right-click “Windows PowerShell” from the search results and select “Run as administrator”. I assume you have Administrative privileges on your Windows 10 or 7 computers, BTW!

Use Ctrl+Shift+Enter key shortcut key combination on your keyboard to launch PowerShell (or any program) with the “Run as Administrator” option!
run powershell as administrator

In the case of Snap-in, Right-click the SharePoint 2013 PowerShell Snap-in and choose “Run as Administrator”.
run as administrator powershell script

Once the PowerShell console is in Administrator mode, you can start typing cmdlets to execute them in Administrator mode.

Switch from Normal User to Administrator in PowerShell

When you open the PowerShell console, it launches in normal user mode by default. To switch to Administrator mode, use the following:

start-process PowerShell -verb runas

This command will launch an elevated Windows PowerShell as an administrator.

How to run a ps1 file in PowerShell as an Administrator?

To run PowerShell scripts as an Administrator, do the following:

  1. Launch the PowerShell console or PowerShell ISE as Administrator using the above “Run as Administrator” method.
  2. Navigate to the script location using the “CD” cmdlet. E.g., “CD C:\Scripts”
  3. Run the PowerShell script by typing its name followed by the “.ps1” extension. E.g., “.\YourScriptName.ps1”

Enable the “Run as Administrator” elevated privilege for PowerShell by default

If you frequently run PowerShell scripts as an admin, you may find it tedious to click “Yes” every time the UAC (User Account Control) prompt appears. Fortunately, there’s a way to bypass the prompt and automatically Open PowerShell in admin mode.

To automatically run the PowerShell script as administrator without a prompt, create a shortcut to your PowerShell console on your desktop.

  1. Right-click the “SharePoint 2013 Management Shell” shortcut (or Windows PowerShell) and click Properties.
  2. Click the “Advanced” button under the Shortcut tab
  3. Enable “Run as Administrator” and click the “OK” button.

powershell script always run as administrator
From now on, you can run PowerShell in elevated mode by double-clicking the new shortcut on your desktop. It opens the PowerShell in Administrative mode always.

Run PowerShell as an administrator in scheduled tasks:

The Task scheduler allows you to schedule scripts to run at specific times or events, allowing them to run with administrative privileges. If you are scheduling a PowerShell script, make sure you select the “Run With the Highest Privileges” check box. Otherwise, your scheduled task may run unattended, invoking a UAC prompt. To do this,

  1. Open the Task Scheduler and create a new task.
  2. In the General tab, give the task a name and description, and select “Run with highest privileges.”
  3. In the Triggers tab, specify when and how often the task should run.
  4. In the Actions tab, select “Start a program” and type “powershell.exe” in the Program/script field. Then, add the path to your PowerShell script in the Add arguments (optional) field.
  5. Finally, click “OK” to save the task.

More on creating a scheduled task to run the PowerShell script is here: How to Run a PowerShell script using Windows Task scheduler?

Handle Run as Administrator within PowerShell script:
Let’s handle it in our PowerShell code, even if you forget to use the “Run as Administrator” option!

Function Check-RunAsAdministrator()
{
  #Get current user context
  $CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
 
  #Check user is running the script is member of Administrator Group
  if($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
  {
       Write-host "Script is running with Administrator privileges!"
  }
  else
    {
       #Create a new Elevated process to Start PowerShell
       $ElevatedProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

       # Specify the current script path and name as a parameter
       $ElevatedProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

       #Set the Process to elevated
       $ElevatedProcess.Verb = "runas"

       #Start the new elevated process
       [System.Diagnostics.Process]::Start($ElevatedProcess)

       #Exit from the current, unelevated, process
       Exit

    }
}

#Check Script is running with Elevated Privileges
Check-RunAsAdministrator

#Place your script here.
write-host "Welcome"

Add this code at the beginning of your script.

Run as a Different User in PowerShell scripts:

$credential = New-Object System.Management.Automation.PsCredential("Domain\UserID", (ConvertTo-SecureString "Password" -AsPlainText -Force))
Start-Process powershell -Credential $credential -NoNewWindow

How to run PowerShell as administrator from the command prompt?

To run PowerShell as an administrator in the command line:

  1. Click on the Windows start icon >> Type “Cmd” to launch the command prompt.
  2. Type : PowerShell to enter into the PowerShell console
  3. Now, Type: Start-Process PowerShell -Verb RunAs

BTW, There could be some more reasons for “The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered” issues, such as:

Another solution to address this issue: Disable UAC! Here is how – How to Disable UAC in Windows Server 2012/2008?

Conclusion

Running a PowerShell script as an administrator is often necessary to execute tasks requiring elevated privileges. By following the steps outlined in this article, you can easily launch PowerShell with administrative rights and run your script. This ensures you have the necessary permissions to perform advanced system modifications, install software, or manage resources as needed.

How do I run PowerShell as an administrator in Windows 10?

From Windows 10 File Explorer, You can select any PowerShell file (.ps1) >> Click on the “File” menu >> Open Windows PowerShell >> Open Windows PowerShell as Administrator.how to run ps1 file in powershell as administrator

How do you run PowerShell as an administrator from a command line?

To run PowerShell as administrator from cmd, Open the command prompt, type PowerShell, and then enter “Start-Process powershell -Verb RunAs”. This will open a new PowerShell window with administrator privileges.

Can you run PowerShell without admin rights?

Yes, you can run PowerShell without admin rights. To do so, simply open the Start menu, type “PowerShell” in the search bar, and click “Windows PowerShell.”. However, any system operation, such as changing system settings, making registry edits, or installing software, etc., requires PowerShell in Admin rights.

How do you detect if PowerShell is running as an administrator?

Use “([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)”. This will return “True” if PowerShell is running as an administrator, and “False” if it is not.

How to run PowerShell as administrator from a batch file?

Use: powershell -noprofile -command “&{ start-process powershell -ArgumentList ‘-noprofile -file C:\script\psfile.ps1’ -verb RunAs}”

How do I run PowerShell as an administrator remotely?

When you execute commands remotely, they are run with administrative privileges because only administrators can execute them remotely in PowerShell.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “How to Run PowerShell Script as Administrator by Default?

  • THANK YOU VERY MUCH !!!

    Reply
  • Hi,

    I have run this PS script:
    Handle Run as Administrator within PowerShell script:
    Let’s handle it in our PowerShell code, even if you forget to use the “Run as Administrator” option!
    ========================================================================
    Added it to my script but it still prompt to click OK/YES. Can we get rid of the prompt?

    Reply
  • I’m new to powerShell
    and find this part of your example confusing
    “Handle Run as Administrator within PowerShell script:”
    # Specify the current script path and name as a parameter
    $ElevatedProcess.Arguments = “& ‘” + $script:MyInvocation.MyCommand.Path + “‘”
    I think of many different ways of what that could mean and i have tried them but i cant get to work.
    A real working example instead if “MyInvocation.MyCommand.Path” would help alot.
    Also it says ” Specify the current script path and name as a parameter” but at the end of your example there is a “.Path” not before but it says “script path and name” really threw me off.
    where does the name go?
    where does the path go?
    is it all just a Path, dir name and ext included?
    there’s no “:” or “\” or “.ps1” in “MyInvocation.MyCommand.Path”
    do i need to change the working Dir?
    what if my script path has spaces?

    Reply
  • Good day. for PNP PS is there something similar available?

    Reply
    • PnP PowerShell is an Add-on to PowerShell! So, launching PowerShell in Admin mode, helps the PnP PowerShell cmdlets to run with elevated privileges.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *