Run scriptblock as different user on local machine

Welcome Forums General PowerShell Q&A Run scriptblock as different user on local machine

This topic contains 2 replies, has 3 voices, and was last updated by

1 month, 2 weeks ago.

  • Author
  • #169396

    Topics: 1
    Replies: 0
    Points: -6
    Rank: Member

    I want to run a scriptblock ($commands) as a different user ($credential) on the same machine where the script runs.

    I tried "Invoke-Command -Credential $Credential -ScriptBlock {$commands}", but it appears that you have to specify  -ComputerName when using -Credential.

    The system is firewalled and I am not allowed to enable PS-remoting.

    Is there a way to to accomplish this?

  • #169435

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    You can use Start-Process and call PowerShell.exe with -credentials and arguments '-c &{your commands}'


    Use Start-Job with Credentials and -ScriptBlock parameter.

  • #169867

    Topics: 1
    Replies: 59
    Points: 328
    Helping Hand
    Rank: Contributor

    you could use -cn $env:computername or  -cn localhost in the invoke-command statement.

    Just make sure that any variables you intend to pass are done as $using:


    $name="bob" would be pased as $using:name and not just $name



You must be logged in to reply to this topic.