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

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.

2

1 Answer

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like