9

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?

12

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.

1

Type the following command:

New-item –type file –force $profile

A file 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.

0

Update the Default $Profile Variable Edit the Default .PS1 file (if not exist, Create it) Add your Script to the .PS1 File

0

There are many ways to do this. A straight forward way is to a profile script in your default powershell home path.

  1. Navigate to $env:UserProfile\Documents\WindowsPowerShell.
    Typically, this expands to C:\Users\<username>\Documents\WindowsPowerShell.
    Create the folders if they do not exist.
  2. 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.ps1 call those scripts.

As a side note, the default profile name should be Microsoft.PowerShell_profile.ps1.
But profile.ps1 also works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.