How can I do an impersonation in PowerShell? I can use advapi32.dll LogonUser, but maybe there is a simpler solution?
4 Answers
If it is for remote connections you might consider the cmdkey.exe tool (native), with either of the following, depending on whether you want the password to be included in the command line or prompted:
cmdkey /add:<servername> /user:<username> /pass:<password>
cmdkey /add:<servername> /user:<username> /pass
I know it's not PowerShell but it a native exe and works where cmdlets don't implement the -Credential
parameter.

- 4,411
- 1
- 24
- 46

- 446
- 4
- 11
There was a similar discussion on this topic a long time ago (well, relative to how long SO exits).
Long story short, use LogonUser, psexec, or PowerShell command that have Identity parameter.

- 1
- 1

- 31,691
- 7
- 68
- 87
You can use the above mentioned methods, and specifically, the PowerShell Get-Credential method worked for me. Also check the Secure-String methods to enable you to get the credential information and use the password information.

- 775
- 6
- 16
This is the best solution.
http://huddledmasses.org/using-alternate-credentials-with-the-filesystem-in-powershell/
I think

- 67
- 2
- 11
- 38