title: run script block as a specific user with Powershell
body: <p>I am not getting anywhere when using Start-Process / Start-Job cmdlets with -Credential $cred</p>
  <h1>Problem</h1>
  <p>I have a service account use in deployment (unattended mode). Previously it has been added to local administrator group.  I want to reduce potential damage I could do by removing this user from admin group and explicitly assign folder permissions to this user.</p>
  <ul>
  <li>I rather get a permission error than execute something that is reaching out by accident.
  Remove-Item &quot;$notdefined\*&quot;</li>
  </ul>
  <p>However in this same powershell script i want to be able to elevate to execute things like:</p>
  <ul>
  <li>sc.exe</li>
  <li>app pool restart
  which requires an admin user.</li>
  </ul>
  <h2>One of my failed attempts</h2>
  <pre><code>$job = Start-Job -ScriptBlock { 
  
  param(
      [string]$myWebAppId
  )
  
  Import-Module WebAdministration
  
  Write-Host &quot;Will get the application pool of: IIS:\Sites\$myWebAppId and try to restart&quot;
  $appPoolName = Get-ItemProperty &quot;IIS:\Sites\$myWebAppId&quot; ApplicationPool 
  Restart-WebAppPool &quot;$($appPoolName.applicationPool)&quot; 
  Write-Host &quot;restart of apppool succeeded.&quot;
  
  } -Credential $cred -ArgumentList @(&quot;appname&quot;)
  
  Write-Host &quot;started completed&quot;
  
  Wait-Job $job
  
  Write-Host &quot;wait completed&quot;
  
  Receive-Job $job -Verbose
  
  Write-Host &quot;receive completed&quot;
  </code></pre>