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?
Run Powershell script when you open Powershell
powershell
powershell
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?
Best Answer
There is a PowerShell script that runs on ps startup, if it exists. The filespec for this script is in the variable
$profile
.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: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 nameExecutionPolicy
and the valueRemoteSigned
.Both have exactly the same effect and will persist across sessions.
The
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 toUnrestricted
to enable unsigned downloaded scripts as well, but this isn't recommended security practice.)