I mainly use powershell to run a server i.e, my first and (mostly) one command is
python manage.py runserver
I seldom have to use other commands.
How can I configure the powershell to open the window with that command. Please note: that I already keep the powershell configured to open in the desired directory.
You can use the PowerShell profile to preload anything useful in your PowerShell session (variables, functions, commands, ...) :
All About PowerShell Profiles
Related
There are a lot of questions already asking similar things but none helped me:
Start new cmd.exe and NOT inherit environment?
Is there a command to refresh environment variables from the command prompt in Windows?
What i'm f.e. after is:
Create a batch- oder powershell script which
installs python to a system where python isn't installed (this works)
after a successful installation, run python with a python script as argument
(It should also work the same way for installing and using other apps like MSVC, git for windows, ...)
After installing python from a console, the path which has been added to the system environment variables isn't reflected in the current cmd instance environment.
But it's there when i start a new cmd or elevated cmd manually from the windows start menu and not from a script.
I tried the following without luck to update the environment of the existing console or create a new instance with the new user environment from script:
start /i/b&exit
start /i/b "%windir%\explorer.exe" "%windir%\system32\cmd.exe"
powershell -Command Start-Process -UseNewEnvironment -Wait -FilePath python -ArgumentList "test.py"
But for UseNewEnvironment it's documented that
the new process starts only containing the default environment
variables defined for the Machine scope. This has the side effect that
the $env:USERNAME is set to SYSTEM. None of the variables from the
User scope are included
and for start /i that
Ignore any changes to the current environment, typically made with SET. Use the original environment passed to cmd.exe
I think both approaches cannot be used for what i'm after.
Is there a simple possibility to automatically run a script in the new environment which the installer created without extensive code or third party binaries?
I have a Powershell script I would like to make "public", meaning I want to be able to execute the script from any folder as you can do from the command prompt.
Is this possible?
You can also add an alias in your local powershell profile
Alias Example in profile
Set-Alias hello C:\scriptlocation\script.ps1
Now anytime you type hello, the script.ps1 will run.
More info on the various profiles that the alias can be saved to.
https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/
You can explore the use of your powershell profile to achieve this. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles.
If the script is just a function or some variables, you can copy and paste the content into your profile.
Alternatively, if the script represents a standalone unit of code you want to keep separate, you could import it into your main profile as such:
get-content -path C:\blahblahblah\scriptName.ps1 -raw | invoke-expression
Finally, if you are writing an "advanced" powershell function, or are trying to do things officially, you could investigate the use of powershell modules as a way to export your function. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
#Lee_Dailey's answer of adding the script to your path is also viable. If you want to add a lot of scripts, one way to do that is to add a folder like C:/PowershellScripts/, to your path, save your scripts there, and then you'll be able to invoke your .PS1 file from anywhere.
Name the script something meaningful ie: awesome.bat Save it in a dir and add the dir to windows env. Command awesome will be globally available.
I use the embedded terminal intellij on Windows and I always run a set of commands as soon as it opens. Commands such as setting aliases etc etc. Is it possible to get intellij to run the automatically?
No, it's not currently supported; please follow IDEA-210036 for updates.
Note that you can set up init script in the AutoRun registry value to execute a set of commands on each cmd.exe start - see
How to run a command on command prompt startup in Windows. But this will affect all your cmd shell instances, not just the cmd.exe in the terminal embedded in WebStorm
I want to run some commands by default every time I open up power shell. Bash has a .bashrc file that it runs as source when it starts up. Is there a way to do this with powershell?
As pointed out by Dai in the comment section, Powershell Profiles sound like the closest equivalent to the .bashrc file.
A PowerShell profile is a script that runs when PowerShell starts. You can use the profile as a logon script to customize the environment. You can add commands, aliases, functions, variables, snap-ins, modules, and PowerShell drives. You can also add other session-specific elements to your profile so they are available in every session without having to import or re-create them.
The question says it all. I want to learn and use powershell as my go to terminal in windows and I want powershell to open if I type Win+R followed by ps. Much like how cmd is used to open command prompt.
You can create a symbolic link (similar to a shortcut) to Powershell with any name you want.
This will create one called ps.exe in the powershell folder, this folder is already listed in PATH so will enable you to run ps from the RUN box like you want.
mklink %SystemRoot%\system32\WindowsPowerShell\v1.0\ps.exe %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Be sure to run the command in an elevated command prompt.
The "default" command for opening PowerShell is powershell. If you want to make it ps, your best bet is to create a batch file named ps.bat containing the single line
#powershell %*
and place it in one of the directories named in your system PATH variable.