How to run copy-item (as admin) inside a non-elevated powershell?

8,289

To Copy-Item as Administrator you should start a PowerShell as Administrator like:

Start-Process PowerShell.exe -ArgumentList "copy source-dir/file.txt dest-dir/" -Wait -Verb RunAs

In my use-case, I wait end of copy, but it not mandatory. The drawback of this solution is that -NoNewWindow is not available then.

Share:
8,289

Related videos on Youtube

mrjayviper
Author by

mrjayviper

Updated on September 18, 2022

Comments

  • mrjayviper
    mrjayviper over 1 year

    so I have a ps1 script which I'm running inside a non-elevated powershell windows.

    But I want to modify my script so I can run copy-item and this needs to write to one of the folders inside c:\windows.

    I've looked at copy-item and there's a parameter called "credential" but I do not know the admin password for my setup.

    I'm hoping something like Debian's "sudo cp -a myfilehere.txt /etc/" would be possible.

    • Ramhound
      Ramhound about 7 years
      The directory you want write to is protected and requires elevated permissions. You would have to disable UAC at the group policy level to avoid this requirement. I don't advise doing so. If you don't have the Administrator creditials then you are out of luck and can't do what you want
    • Ramhound
      Ramhound about 7 years
      You can't run a cmdlet as an Administrator in a non-elevated PowerShell command prompt.