How to have a command run when opening PowerShell?  


  1. Posts : 16
    Windows 10 Home (64-bit)
       #1

    How to have a command run when opening PowerShell?


    Hi there everyone. I did a quick search of the forums but didn't see anything covering this. I recently found this program called winfetch that displays the Windows logo in ASCII along with system info. It's a single command to run, and I was wanting to configure PS 7 to run it automatically when first opened. In Linux and WSL you can do this by editing the bashrc file in the home directory, but I'm new to PowerShell and am not sure of how to get this to work. Any help would be greatly appreciated!
      My Computers


  2. Posts : 16,224
    Windows 10 Home x64 Version 22H2 Build 19045.3693
       #2

    I use shortcuts of my own making when opening command windows and powershell windows. For example,
    Code:
    %windir%\system32\cmd.exe /k "(cd /d D:\Desktop) & (Title PS) & (powershell)"
    - This opens a powershell window, changes its current path to D:\Desktop and sets the TitleBar to start with PS.

    Perhaps
    Code:
    %windir%\system32\cmd.exe /k "("C:\WHATEVER\winfetch") & (powershell)"
    can do what you want?

    Or perhaps you could start with the standard PS call
    Code:
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    and tailor that to your needs? Possibly something like
    Code:
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe &"C:\WHATEVER\winfetch"
    - If you end up needing multiple commands then you add a ; to the powershell command [rather than the & you would add for cmd window commands]. See Execute multiple Powershell commands on one line | Thomas' Miniblog


    Best of luck,
    Denis
      My Computer


  3. Posts : 16
    Windows 10 Home (64-bit)
    Thread Starter
       #3

    Thanks Dennis. I'll give it a shot and let you know how it works out.
      My Computers


  4. Posts : 65
    Win10 Home 64-bit, MX-Linux
       #4

    That's what the Powershell profile folder(s) are for. They contain scripts that are executed automatically when Powershell
    starts. The profiles for your version of Powershell are discussed here:
    PowerShell 7 Profile Paths and Locations - Ridicurious
      My Computer


  5. Posts : 16
    Windows 10 Home (64-bit)
    Thread Starter
       #5

    kreemoweet said:
    That's what the Powershell profile folder(s) are for. They contain scripts that are executed automatically when Powershell
    starts. The profiles for your version of Powershell are discussed here:
    PowerShell 7 Profile Paths and Locations - Ridicurious
    Thanks everyone for helping. I got it sorted out by using the profile scripting feature. Other sources that helped are linked below. Basically ran notepad $PROFILE and created the profile.ps1 script and added winfetch to that. Now it runs in as desired in the normal PS window, PS7, and in Windows Terminal.

    https://docs.microsoft.com/en-us/pow...powershell-7.1
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:01.
Find Us




Windows 10 Forums