Table of Contents
Our script will open the Windows Calculator application at Windows startup and login based on the solution you will choose from. To meet your requirements, you can replace the script in
file.ps1
with your script.
Using Startup Folder
Use the Startup
folder to run the PowerShell script at startup. To do this, we must follow this section’s step-by-step instructions.
Create .ps1
File
Open Notepad, write the following command, and save the file with any name of your choice but with the .ps1
extension. You can store this PowerShell script file at any location on your local machine; we saved this file with the file.ps1
name at the E:\Test location.
1 2 3 |
powerShell "c:\windows\system32\calc.exe" |
This command would open the calculator on the local computer.
Create .cmd
File
Launch Notepad, write the following commands, and save the file with a name but with the extension .cmd
at %AppData%\Microsoft\Windows\Start Menu\Programs\Startup path. We named this file Startup.cmd
and placed it in the Startup
Folder.
While saving the .cmd
file, don’t forget to select All Files
for the Save as type:
option and ANSI
for the Encoding
option; see the screenshot after the following script for clear understanding.
1 2 3 4 |
PowerShell -Command "Set-ExecutionPolicy Unrestricted" -Scope CurrentUser >> "%TEMP%\StartupLog.txt" 2>&1 PowerShell E:\Test\file.ps1 >> "%TEMP%\StartupLog.txt" 2>&1 |
The above script did two things. First, it sets the execution policy to Unrestricted
, and the second is to use PowerShell to run the .ps1
script file in the given path. Let’s break down the above commands to learn them in detail.
The first command:
- Runs the PowerShell with the
-Command
parameter to execute theSet-ExecutionPolicy Unrestricted
command, which modifies the PowerShell execution policy to let us run unsigned scripts. - The
-Scop
parameter applies changes to the current user instead of the entire system. - The redirection operator represented by
>>
redirects the command output (if any) to theStartupLog.txt
file in the user’s%TEMP%
directory. The>>
operator appends the output to the end of theStartupLog.txt
file if it already exists; otherwise, it creates it. - The
2>&1
operator redirects the command’s error output (2
) to the exact file as the standard output (&1
).
The second command:
- Runs PowerShell with the E:\Test\file.ps1 script as an argument. You might have a different path if you have stored your
.ps1
file in a different location on your PC. - The redirection operator (
>>
) does the same as in the first command. - The
2>&1
operator also does the same thing as in the first command.
Overall, both commands use the >>
and 2>&1
operators to capture the errors and output of the PowerShell command or script and write them to a StartupLog.txt
file in the user’s %TEMP%
directory.
Restart Your PC
Now, you have the .ps1
file and .cmd
file at the required locations, so it’s time to restart your computer. Once the computer is on, the script will be executed and open the Calculator app for you.
Using Task Scheduler
Use Task Scheduler to run PowerShell script at startup. Following the below steps to do it.
Open Task Scheduler
Press Windows+R key from the keyboard, type taskschd.msc
in the Run
prompt and hit OK
. It will open the Task Scheduler window for you. See the following screenshot.
Otherwise, you can use the Windows search menu to open Task Scheduler.
Create a Task
-
In the
Task Scheduler
window, clickCreate Task
as demonstrated below. -
Ensure you are on the
General
tab in theCreate Task
window. Then, write the name of the task (step-1), select theRun only when the user is logged on
radio button (step-2), check theRun with highest privileges
checkbox (step-3), and click on theActions
tab (step-4). -
Once you are on the
Actions
tab, click on theNew
button (step-2), make sure theStart a program
option is selected forActions:
option (step-3), writepowershell.exe
forProgram/script
option (step-4), addE:\Test\file.ps1
as the value ofAdd arguments (optional)
option (step-5) and clickOK
button (step-6). Remember, you can specify your arguments for step-5. -
Now, click on the
Triggers
tab (step-1), click on theNew
button (step-2), selectAt log on
forBegin the task:
option (step-3), choose theAny user
orSpecific user
based on your requirements (step-4), clickOK
(step-5), and clickOK
again (step-6).Your task must be listed in the
Task Scheduler
window (see the following); if it is there, you have done everything right, close this window.
Log Out and Login Your PC
Log out and log in again to your computer. You don’t need to reboot the computer to run this script. This script will open the Windows Calculator application.
Further reading:
Using Registry Editor
Use Task Scheduler to run PowerShell script at startup. Following the below steps to do it.
Open Registry Editor
Press Windows+R key from the keyboard, type regedit.msc
in the Run
prompt and hit OK
. It will open the registry editor window.
Add Entry to the Windows Registry Editor
-
Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run in the registry editor (step-1).
-
Right-click the
Run
key and chooseNew->String Value
(step-2). -
Give a new entry a meaningful name as we named it
PSfile
. Note that this name must be unique; otherwise, you will get an error. -
Double-click on the new entry (
PSfile
) to open theEdit String
window. -
In the
Edit String
window, you can confirm the new entry’s name (step-3). For theValue data
field, enter thepowershell "E:\Test\file.ps1"
command (step-4); you can replace the path to point your PowerShell script file. Remember to surround the path with double quotes. -
Click
OK
to exit theEdit String
window (step-5). -
Close the Registry Editor.
Restart Your PC
Restart your computer to run the script automatically every time Windows starts up. This script will open the Windows Calculator application for you.