title: Restart System and Run Powershell Script without logging in
body: <p>I have multiple restarts between script blocks, the problem is on every restart the user is required to key in their credentials, and log in.</p>
  
  <p>Is there any way where once the script is being executed it can login to system without username and password? We would want to hardcode username and password in the script itself.</p>
  
  <p>The script helps in achieving the following:</p>
  
  <ol>
  <li>Run Script Block</li>
  <li>Restart</li>
  <li>Run Next Script Block</li>
  </ol>
  
  <p>But this requires user to log in after restart. Until then the process is under halt. I am looking for a way to automate the login process as well... I know it creates security threat, but this is something that is necessary for the process I am supposed to achieve.</p>
  
  <pre><code>#Code which will run script with admin privileges
  If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
  
  {   
  $arguments = "&amp; '" + $myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
  }
  
  # Temp Folder
  if (!(Get-Item d:\temp -ea ignore)) { mkdir d:\temp }
  
  $dropperscript = 'D:\temp\dropper.ps1'
  
  $dropper = @'
  #############################################
  ###        Configuration Variables        ###
                                              #
  # Put any variables you'll use here
                                              # 
  ###                                       ###
  #############################################
  
  # Static Variables
  $countfile = 'd:\temp\bootcount.txt'
  $bootbatch = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\dropper.bat'
  $dropperscript = 'd:\temp\dropper.ps1'
  
  #################
  ##### Setup #####
  
  # Bootstrap Batch
  if (!(Get-Item $bootbatch -ea ignore)) {
  $x='PowerShell.exe -Command "&amp; {Start-Process PowerShell.exe -ArgumentList '''
  $y=' -ExecutionPolicy Bypass -File "D:\temp\dropper.ps1""'' -Verb RunAs}"'
  
  $x+$y| Out-File $bootbatch -Encoding 'OEM'
  
  }
  
  # Boot Count
  if (Get-Item $countfile -ea ignore) {
      [int]$bootcount = Get-Content $countfile
      if ($bootcount -match "^\d{1,2}$") { ([int]$bootcount) ++ }
      else { $bootcount = 1 }
  }
  else { $bootcount = 1 }
  $bootcount | Out-File $countfile
  
  
  switch ($bootcount) {
  
      1 {
     $File="D:\temp\Log.txt"
      Get-Date | Out-File $File -Append 
     Get-Process | Out-File $File -Append 
          Restart-Computer -Force
          ##################################################
          ###############     --REBOOT--     ###############
      }
  
      2 {
         $File="D:\temp\Log.txt"
      Get-Date | Out-File $File -Append 
     Get-Process | Out-File $File -Append 
          Restart-Computer -Force
          ##################################################
          ###############     --REBOOT--     ###############
      }
  
      3 {
          $File="D:\temp\Log.txt"
      Get-Date | Out-File $File -Append 
      Get-Process('\\itdsl\MSNPLAT\Gold\MSNPATCH\msnpatch.exe') | Out-File $File -Append 
  
         Restart-Computer -Force
          ##################################################
          ###############     Restart    ################
      }
  
      4{
      $File="D:\temp\Log.txt"
      Get-Date | Out-File $File -Append 
      Get-Process| Out-File $File -Append 
  
         Restart-Computer -Force
          ##################################################
          ###############      --END--      ################
   }
  
      default {
          # Dropper is complete; clean up
          rm $countfile
          rm $bootbatch
          rm $dropperscript
      }
  }
  '@
  
  # Drop and run Dropper
  
  $dropper | Out-File $dropperscript -Encoding 'OEM'
  
  Invoke-Expression $dropperscript
  </code></pre>