To run a command on PowerShell startup, the most common and flexible method is to use a PowerShell profile. A profile is a script that runs automatically every time PowerShell starts, allowing you to customize your environment with commands, functions, aliases, and variables.
Understanding PowerShell Profiles
A PowerShell profile is simply a .ps1
script file that PowerShell executes when it launches. This script can contain any valid PowerShell commands, letting you define your preferred settings, load modules, create custom functions, or run specific tasks automatically.
There are several types of PowerShell profiles, each affecting different scopes:
Profile Path Variable | Scope | Description |
---|---|---|
$PROFILE.CurrentUserCurrentHost |
Current User, Current Host | The most common profile. Applies to the current user and the current PowerShell host application (e.g., PowerShell console, VS Code, PowerShell ISE). |
$PROFILE.AllUsersCurrentHost |
All Users, Current Host | Applies to all users on the system for the current PowerShell host. Requires administrative privileges to edit. |
$PROFILE.CurrentUserAllHosts |
Current User, All Hosts | Applies to the current user across all PowerShell host applications. |
$PROFILE.AllUsersAllHosts |
All Users, All Hosts | Applies to all users and all PowerShell host applications. Requires administrative privileges. |
The $PROFILE
automatic variable always contains the full path to your current user, current host profile.
Locating and Creating Your PowerShell Profile
To find out if you already have a profile and its path, simply type $PROFILE
in your PowerShell console:
$PROFILE
If the profile file does not exist at the specified path, PowerShell will not create it automatically. You can create it using the New-Item
cmdlet:
if (-not (Test-Path $PROFILE)) {
New-Item -Path $PROFILE -ItemType File -Force
Write-Host "PowerShell profile created at: $PROFILE"
} else {
Write-Host "PowerShell profile already exists at: $PROFILE"
}
Editing Your PowerShell Profile
Once the profile file exists, you can open it with any text editor. A common way is to open it directly from PowerShell:
notepad $PROFILE
# Or for VS Code users:
code $PROFILE
Inside this file, you can add any commands you want to run on startup.
Examples of commands to add to your profile:
- Set an alias:
Set-Alias gs Get-Service
- Load a module:
Import-Module Az
- Change the default location:
Set-Location C:\Projects
- Display a custom welcome message:
Write-Host "Welcome back, $($env:USERNAME)!" -ForegroundColor Green
- Run another script:
& "C:\Scripts\MyStartupScript.ps1"
(Using
&
(call operator) ensures the script runs with the current scope.)
Save the file, and the next time you open PowerShell, your added commands will execute.
Advanced Startup Scenarios and Considerations
While PowerShell profiles are the primary method for custom startup commands, other considerations and methods exist for more complex or restricted environments.
Enabling Script Execution in Restricted Environments
In certain managed or highly secure environments (e.g., corporate networks, systems with strict endpoint protection), simply placing a script in your PowerShell profile might not be sufficient due to stringent security policies. These environments often implement mechanisms to control which scripts are allowed to execute.
To permit specific PowerShell scripts to run, you might need to explicitly add them to an "allow list" or whitelist within your system's security or management console. This typically involves navigating to a designated 'allow list tab' where you would specify the name of your PowerShell script, such as mypowershell.ps1
. For robust security and to prevent conflicts with scripts of the same name in different locations, it is highly recommended to include the full or partial path to the script, for example, partialpath\mypowershell.ps1
. This ensures that only approved scripts are permitted to execute, providing an additional layer of control over what runs during PowerShell startup.
PowerShell Execution Policies
PowerShell's execution policy is a security feature that controls the conditions under which PowerShell loads configuration files and runs scripts. Before your profile script can run, your execution policy must allow it.
You can check your current execution policy with:
Get-ExecutionPolicy
Common policies include:
- Restricted: No scripts can run.
- RemoteSigned: Scripts created on your computer can run, but scripts downloaded from the internet must be signed by a trusted publisher. This is often a good balance for security and functionality.
- Unrestricted: All scripts can run. This is generally not recommended for security reasons.
You can change the execution policy using Set-ExecutionPolicy
. For example, to set it to RemoteSigned
for the current user:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
For more details, refer to the official Microsoft documentation on About Execution Policies.
Using Scheduled Tasks
For commands that need to run at specific times, with different user contexts, or even before a user logs in, Windows Task Scheduler is a powerful tool. You can configure a scheduled task to run a PowerShell script or command at system startup, user logon, or at a recurring interval.
- Open Task Scheduler (search for it in the Start menu).
- Go to
Action > Create Basic Task...
orCreate Task...
. - Set the trigger (e.g., "When the computer starts," "When a specific user logs on").
- For the action, choose "Start a program" and specify:
- Program/script:
powershell.exe
- Add arguments (optional):
-NoProfile -ExecutionPolicy Bypass -File "C:\Path\To\YourScript.ps1"
(or-Command "Your-Command-Here"
) - Using
-NoProfile
ensures your profile script doesn't run twice if your script also opens PowerShell, and-ExecutionPolicy Bypass
temporarily overrides the policy for the task.
- Program/script:
Registry Startup Entries
For system-wide commands that need to run whenever Windows starts, you can add entries to the Windows Registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
or HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
. While this method works for any executable, it's generally less preferred for PowerShell-specific customizations that don't need to run for all users or at a system level, as PowerShell profiles offer more flexibility and are easier to manage for individual users.