0

I have the following powershell script:

(Get-Content C:/sample.txt -TotalCount 1) | Set-Content C:\sample.txt

I would like the above script to execute when system boot.

Can someone please advise the best option to do so? Thank you Manu

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Manu2287
  • 27
  • 5
  • Possible duplicate of [how to run PowerShell Script when computer starts?](http://stackoverflow.com/questions/20575257/how-to-run-powershell-script-when-computer-starts) – Lance U. Matthews Oct 10 '16 at 21:54

2 Answers2

3

you can create a scheduled task and configure it to run the script when the server boots. Every time server starts, it will start the script.

enter image description here

Vladimir Bundalo
  • 645
  • 8
  • 18
0

Best solution to solve the situation: -create a .cmd file to execute automatically your script at system boot, with following script: PowerShell -Command "Set-ExecutionPolicy Unrestricted" | powershell.exe -noexit C:\cldscripts\sample.ps1

Copy and save your .cmd file in the following path: C:\Users\youruser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup Reboot your system and the scrip will automatically run.

Another way I found, is to use the grouop local editor: Run the gpedit.msc and enter. In the left pane tree choose windows settings under Computer configuration, double click on the feature "Scripts (Startup/Shutdown)" and add your script to run automatically at system boot or shutdown.

Best Regards

Manu2287
  • 27
  • 5
  • 1
    As @23Stinger noted, a script run from the `Start Menu\Programs\Startup` directory is a login script, not a startup script. The script will not run until that user logs in, and it will execute under that user's account. Also, it is possible (if not preferable) to invoke a PowerShell script without permanently changing the machine execution policy using the `-ExecutionPolicy` parameter: `powershell.exe -ExecutionPolicy Unrestricted -NoExit C:\cldscripts\sample.ps1`. That PowerShell session will run with the specified execution policy but the system execution policy will remain unchanged. – Lance U. Matthews Oct 10 '16 at 21:50