Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PackageManagement\Install-Package fails with "Administrator rights are required to install or update." when run as administrator #12777

Closed
rifuller opened this issue May 25, 2020 · 23 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a

Comments

@rifuller
Copy link

rifuller commented May 25, 2020

Steps to reproduce

PS C:\Users\rfull> $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
PS C:\Users\rfull> $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
True
PS C:\Users\rfull> PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
Install-Package: C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:9709
Line |
9709 |  … talledPackages = PackageManagement\Install-Package @PSBoundParameters
     |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Administrator rights are required to install or update. Log on to the computer with an account that
     | has Administrator rights, and then try again, or install by adding "-Scope CurrentUser" to your
     | command. You can also try running the Windows PowerShell session with elevated rights (Run as
     | Administrator).

Separately but maybe something is busted on my machine:

PS C:\Users\rfull> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm

Confirm
Are you sure you want to perform this action?
Performing the operation "Set-ExecutionPolicy" on target "RemoteSigned".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
Set-ExecutionPolicy: Could not find file 'C:\Users\rfull\OneDrive\Documents\PowerShell'.

Expected behavior

posh-git is installed and execution policy is updated.

Actual behavior

Install-Package: C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:9709
Line |
9709 |  … talledPackages = PackageManagement\Install-Package @PSBoundParameters
     |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Administrator rights are required to install or update. Log on to the computer with an account that
     | has Administrator rights, and then try again, or install by adding "-Scope CurrentUser" to your
     | command. You can also try running the Windows PowerShell session with elevated rights (Run as
     | Administrator).

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.1
PSEdition                      Core
GitCommitId                    7.0.1
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@rifuller rifuller added the Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a label May 25, 2020
@rifuller
Copy link
Author

Issue is caused by controlled folder access being enabled.

@ScottHutchinson
Copy link

ScottHutchinson commented Jul 5, 2020

This worked for me. Windows Security settings...Virus & threat protection...Virus & threat protection settings...Manage settings...Manage controlled folder access...Allow an app through Controlled folder access...Add an allowed app...Recently blocked apps...select pwsh.exe (C:\Program Files\PowerShell\7) from the list.

@rifuller
Copy link
Author

rifuller commented Jul 6, 2020

This worked for me. Windows Security settings...Virus & threat protection...Virus & threat protection settings...Manage settings...Manage controlled folder access...Allow an app through Controlled folder access...Add an allowed app...Recently blocked apps...select pwsh.exe (C:\Program Files\PowerShell\7) from the list.

I disabled it temporarily. If I had to take a guess it'd be the profile folder under Documents that's being blocked (if that is indeed created during install).

@shinieki
Copy link

I experienced exactly the same issue. By turning off the controlled folder access under Windows Defender, I was able to install the modules as described by other users. Thanks for the info, really helped.

@daniel-ayers
Copy link

Why was this closed? Is it Microsoft policy that to use PowerShell, controlled folder access must be disabled? Given that an Administrator can install software on a Windows system even with CFA enabled, shouldn't PS Install-* give the same experience? And if not, isn't that a bug that should be fixed?

@JoeBrunsTR
Copy link

JoeBrunsTR commented Jan 26, 2021

Uninstalling my client's A/V entirely, rebooting, checking "Windows Security settings...Virus & threat protection...Virus & threat protection settings...Manage settings...Manage controlled folder access", controlled folders was off, still refuses to upgrade. Uninstall fails too. Sucks to be in this state.

@mhectorgato
Copy link

mhectorgato commented Feb 23, 2021

Experiencing similar issue -- started PS7 as Administrator, entered my credentials to allow Admin access. In PS, I ran the following:

Install-Module AZ -AllowClobber -Force

Am getting the following:

Administrator rights are required to install or update. Log on to the computer with an account that has Administrator rights, and then try again, or install by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).

Controlled Folders was already disabled in Virus setting.

@solanki-kushal
Copy link

solanki-kushal commented Mar 14, 2021

Have tried all the options but nothing seems to work for me. Have even tried uninstalling using steps mentioned in link (https://docs.microsoft.com/en-us/powershell/azure/uninstall-az-ps?view=azps-5.6.0). Finally had to delete all the Az folders from C:\Users\kusha\OneDrive\Documents\PowerShell\Modules and copy from C:\Program Files\WindowsPowerShell\Modules which resolved the issue for me. You can run command $env:PSModulePath -split ";" to get all the modules path.

How to perform a complete uninstall of Azure PowerShell

@sandshadow
Copy link

I hit this issue as well when trying to install the Azure (Az) module and wasn't able to disable Controlled Folder Access even temporarily because of group policy on my work machine. I was able to work around the issue by installing through a msi instead of going through PSGallery.

@brandonh-msft
Copy link

Having same problem here, Controlled Folder Access is already disabled @requilium

@rkeithhill
Copy link
Collaborator

Like @kushal933 - uninstalling all previous versions of a module from $HOME\Documents\PowerShell\Modules worked for me.

My latest run-in with this error occurred while upgrading to PSReadLine 2.2.0-beta.3. I had to close all pwsh/pwsh-preview processes and delete the $HOME\Documents\PowerShell\Modules\PSReadLine. Then I was able to install the latest PSRL beta.

@brandonh-msft
Copy link

brandonh-msft commented Jun 4, 2021

I nixed all the Modules from disk but now receive:

> Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force -AllowClobber                                                
WARNING: The version '2.3.0' of module 'Az.Accounts' is currently in use. Retry the operation after closing the applications.                                                                                                 
WARNING: The version '1.1.1' of module 'Az.Advisor' is currently in use. Retry the operation after closing the applications.                                                                           

I've tried killing processes, etc, but can't get around it.

@rkeithhill
Copy link
Collaborator

Ensure there are no pwsh/pwsh-preview processes running. Then start pwsh/pwsh-preview with the -noprofile parameter: pwsh.exe -noprofile. Then try the install.

@brandonh-msft
Copy link

brandonh-msft commented Jun 8, 2021

no dice
image
image

@rkeithhill
Copy link
Collaborator

I would choco install sysinternals, and use the handle.exe utility (from an elevated console) to see which process has those files in use. I mean it's probably PowerShell but maybe it's another instance of PowerShell?

@brandonh-msft
Copy link

thing is, the modules don't even exist on my machine:
image
image
image

@rkeithhill
Copy link
Collaborator

I would give handle.exe a go just to see if these files are somewhere you haven't looked e.g. C:\Program Files\PowerShell\Modules. FYI $env:PSModulePath will show you all the paths where PowerShell looks for modules.

@brandonh-msft
Copy link

brandonh-msft commented Jun 8, 2021

I would give handle.exe a go just to see if these files are somewhere you haven't looked e.g. C:\Program Files\PowerShell\Modules. FYI $env:PSModulePath will show you all the paths where PowerShell looks for modules.

Thanks for the $env:PSModulePath tip! Found the Az modules in C:\Program Files\WindowsPowerShell\Modules and was able to delete them all just fine (read: they weren't in use). Then ran Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force -AllowClobber as non-admin without issue. 🎉

@kobulloc-MSFT
Copy link

kobulloc-MSFT commented Jun 8, 2021

Az module falsely throws Admin rights required error:
Trying to install az module when it already exists will give this error (and it appears to be the cause for this issue).
https://docs.microsoft.com/en-us/azure-stack/operator/powershell-install-az-module?view=azs-2102#error-thrown-when-installing-the-az-modules

  • Applicable: This issue applies to 2002 and later
  • Cause: When installing the module from an elevated prompt, an error is thrown. The error says, Administrator rights required.
  • Remediation: Close your session and start a new elevated PowerShell session. Make sure there isn't an existing Az. Accounts module loaded in the session.
  • Occurrence: Common

Private cloud
Someone working with the AZ modules to work against a private cloud may want to run the modules in an isolated environment such as a container. You can find instructions here:
https://docs.microsoft.com/en-us/azure-stack/user/azure-stack-powershell-user-docker?view=azs-2102&tabs=az

Learn how to install PowerShell for Azure Stack Hub.

@mmousa8189
Copy link

This worked for me. Windows Security settings...Virus & threat protection...Virus & threat protection settings...Manage settings...Manage controlled folder access...Allow an app through Controlled folder access...Add an allowed app...Recently blocked apps...select pwsh.exe (C:\Program Files\PowerShell\7) from the list.

@ScottHutchinson

thanks, it works for me.

@tranj
Copy link

tranj commented Oct 28, 2021

I found that powershell was installing files in my Documents directory. I also had OneDrive syncing that directory, which somehow interfered with the powershell process. Deactivating/uninstalling OneDrive fixed my issue.

mavaddat added a commit to mavaddat/PowerShellEditorServices that referenced this issue Feb 18, 2022
PowerShell requires admin privileges for installing or updating PackageManagement modules. See PowerShell/PowerShell#12777
@danielschaeffer
Copy link

This worked for me. Windows Security settings...Virus & threat protection...Virus & threat protection settings...Manage settings...Manage controlled folder access...Allow an app through Controlled folder access...Add an allowed app...Recently blocked apps...select pwsh.exe (C:\Program Files\PowerShell\7) from the list.

This worled for me too! Thanks a lot.

@itpropro
Copy link

itpropro commented Sep 2, 2022

Be careful with allowing PowerShell as an exclusion, it basically opens the door for all PowerShell based malware (99%).
Just disable controlled folder access or add the exclusion, install the module and enable/remove the exclusion again.
This way you don't keep a potential attack surface open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
Projects
None yet
Development

No branches or pull requests

16 participants