Run PowerShell Script as Administrator (Elevated Privileges) by Default

It’s a common SharePoint Administrator’s pitfall – Forget to run PowerShell script using “Run as Administrator” option, failing so could lead to many *weird* issues while running PowerShell scripts in SharePoint, such as: “The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.”.

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


The solution is pretty simple! Just right-click the SharePoint 2013 PowerShell Snap-in and choose the option “Run as Administrator”.
run as administrator powershell script

Enable “Run as Administrator” elevated privilege for SharePoint 2013 Management Shell by default:

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

  • Right-click the “SharePoint 2013 Management Shell” shortcut and click Properties
  • Click “Advanced” button under Shortcut tab
  • Enable “Run as Administrator” and click on “OK” button.

powershell script always run as administrator
Now you can run PowerShell in elevated mode by simply double-clicking the new shortcut on your desktop.

Run PowerShell as an administrator in scheduled tasks:

If you are scheduling a PowerShell script, make sure you select the “Run With the Highest Privileges” check box. Otherwise, your scheduled task may fail to run unattended, which invokes a UAC prompt.

Handle Run as Administrator within PowerShell script:
Let’s handle it in our PowerShell code itself, even 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
       Write-host "Script is running with Administrator privileges!"
       #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

       #Exit from the current, unelevated, process


#Check Script is running with Elevated Privileges

#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 line?

To run PowerShell as an administrator in the command line:

  1. Type : PowerShell to enter into PowerShell console
  2. 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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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