1

I have a Powershell script that requires Admin elevation to setup an Azure file share among other things. The final script command uses New-PSDrive to map the file share to a drive letter. My expectation is that the new drive letter be accessible from Windows Explorer. I'm aware that using New-PSDrive from an elevated context only maps the drive in that context so it's not available to my user account.

How do I execute a Powershell script/process as the current user from an elevated script?

2
  • wouldn't using the -persist and -scope Global switch of New-PSDrivemake the drive available for you too?
    – SimonS
    Jul 2, 2019 at 6:27
  • They work, but only in Admin context. No drive letter shows in in Explorer etc
    – Geordie
    Jul 2, 2019 at 6:35

2 Answers 2

2

You cannot change user context without providing user credentials. You can get the logged on user easily enough, but you'd never get their password, unless they enter it.

Invoke-Command -FilePath "yourPath\yourScript.ps1" -ComputerName yourServer -Credential Get-Credential

Secondly, if you are running the remotely, the user would never see this since it can only be run in the original context you are in.

PowerShell does not natively allow you to do this as it is a Windows Security boundary. If you really need to do this, look to using MS SysInternals PSExec.

Or look to do stuff in a scheduled task.

1
  • Thanks yeah realizing now how separate Admin privileges are. I always thought since I was local Admin it was just an extra security check.
    – Geordie
    Jul 2, 2019 at 6:34
0

I was thinking about this all wrong since I default to running everything as Admin. I split the script into two separate scripts, one for code that requires Admin rights and one that requires user rights. I call the user rights script which checks and errors if in Admin context. Then I can execute the Admin script from the user script in it's own elevated prompt, wait and I'm back to user context.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.