Search code examples
powershellstartup

Running a powershell script (.ps1) on startup


Scratching my head with this one. Very new to Powershell also.

GOAL: Upon machine start up Powershell Script launches and asks for a computer name, then adds it to the domain, does a clean up and restarts the machine.

Here is my script:

$getName = Read-Host -Prompt "Enter the new name for this computer"
$user = "mdt_join"
$password = Read-Host -Prompt "Enter password for $user" -AsSecureString
$username = "bc\mdt_join"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)

Add-Computer -NewName $getName -DomainName DOMAIN -OUPath "OU=Managed Stations,DC=DC,DC=DC,DC=DC" -Credential $credential -restart

Rename-Computer –NewName $getName

Remove-Item C:\Users\ladmin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd

Remove-Item $MyINvocation.InvocationName

This script works if I run it manually but when I use this startup.cmd

REM   Attempt to set the execution policy by using PowerShell version 2.0 syntax.
PowerShell -Version 2.0 -ExecutionPolicy Unrestricted .\script1.ps1 >> "%TEMP%\StartupLog.txt" 2>&1

IF %ERRORLEVEL% EQU -393216 (
   REM   PowerShell version 2.0 isn't available. Set the execution policy by using the PowerShell version 1.0 calling method.

   PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
   PowerShell .\startup.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
)

REM   If an error occurred, return the errorlevel.
EXIT /B %errorlevel%

I dont get the same result. It just reverts back to the PS C:> bit. I needed the startup.cmd to run it as unrestricted.

Can someone look over this and see where I am going wrong? Go easy with me - I only started dabbling in powershell yesterday.


Solution

  • Execute PowerShell command below to run the PowerShell script .ps1 through the task scheduler at user login.

    Register-ScheduledTask -TaskName "TASKNAME" -Trigger (New-ScheduledTaskTrigger -AtLogon) -Action (New-ScheduledTaskAction -Execute "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-WindowStyle Hidden -Command `"& 'C:\PATH\TO\FILE.ps1'`"") -RunLevel Highest -Force;
    

    -AtLogOn - indicates that a trigger starts a task when a user logs on.

    -AtStartup - indicates that a trigger starts a task when the system is started.

    -WindowStyle Hidden - don't show PowerShell window at startup. Remove if not required.

    -RunLevel Highest - run PowerShell as administrator. Remove if not required.

    P.S.

    If necessary execute PowerShell command below to enable PowerShell scripts execution.

    Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Unrestricted -Force;
    

    Bypass - nothing is blocked and there are no warnings or prompts.

    Unrestricted - loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the internet, you're prompted for permission before it runs.