PowerShell: Run PowerShell As Another User

# Set credential
$adminUsername='domain\adminDude'
$adminPassword='whatpassword?'
$GLOBAL:adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force)

# Test pinging some domain
$domain='google.com'
$testJob=Start-Job -ScriptBlock {
    param($x)
    ping $x
} -Credential $adminCredential -Args $domain
Wait-Job $testJob|out-null 
$jobResult=Receive-Job $testJob

$jobResult|write-host

Leave a Reply

Your email address will not be published. Required fields are marked *