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

powershellwindowswindows 10

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.

Best 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.

Related Question