Is it possible to run a Powershell script when you run Powershell? As in, double click the Powershell icon and open the window. Is there some type of "auto-run" setting somewhere?
There is a PowerShell script that runs on ps startup, if it exists. The filespec for this script is in the variable
You can use PowerShell commands to check to see if this script file exists, to create it if it doesn't, and to edit it with notepad. Here's the how-to guide.
Note that in recent (~2020 and later) versions, PowerShell will no longer run unsigned scripts (not even
$profile!) by default. If you just follow the old instructions such as in that how-to guide, when you open a new PowerShell, you'll see an error message like:
. : File C:\[..]\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
Signing your profile once — let alone every time you change it — is probably not realistic, so you must change the Execution Policy to allow it.
To do this, you can either:
Run the following command as Administrator:
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
or, use RegEdit to modify
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell, adding a new REG_SZ with the name
ExecutionPolicyand the value
Both have exactly the same effect and will persist across sessions.
RemoteSigned policy requires that scripts downloaded from elsewhere be signed, but scripts you create locally on your machine (such as
$profile) can run without a signature. (You could also set the policy to
Unrestricted to enable unsigned downloaded scripts as well, but this isn't recommended security practice.)
1here's another answer that explains why
RemoteSignedis usually safe to use. Also, I had to do
LocalMachineto get it to work. Dec 18, 2021 at 18:53
Type the following command:
New-item –type file –force $profile
Microsoft.PowerShell_profile.ps1 will be created in
C:\Users\<username>\Documents\WindowsPowerShell\ for PowerShell 5 and older or
C:\Users\<username>\Documents\PowerShell\ for PowerShell 6 Core (this folder will be automatically created).
Then edit this file and you can add personalized PowerShell functions or load modules or snap-ins...
Now when you run your powershell console,
Microsoft.PowerShell_profile.ps1 will be triggered.
There are many ways to do this. A straight forward way is to a profile script in your default powershell home path.
- Navigate to
Typically, this expands to
Create the folders if they do not exist.
- Create a file named
profile.ps1. You can write your script in this file, and this file will be executed each time you start powershell as the user. If you have the script somewhere else, you can have
profile.ps1call those scripts.
As a side note, the default profile name should be
profile.ps1 also works.
To run an arbitrary script on PowerShell session startup, use the following command:
PowerShell -NoExit -File PathToScript.ps1
That's what I am looking for.
-NoExitis not in
powershell /?Nov 30, 2021 at 2:48
@BaiJiFeiLong I'm using PowerShell 5.1, it does have -NoExit flag. Newer versions should have an analog too, try
-noe. Dec 1, 2021 at 3:44
Update the Default $Profile Variable Edit the Default .PS1 file (if not exist, Create it) Add your Script to the .PS1 File