Reputation: 800
I am trying to create workin environment (PATH and aliases) using powershell script.
powershell.exe -Command c:\workspace\script\profile.ps1 -NoExit
Inside the script, I set-up an alias:
Set-Alias npp "C:\Program Files (x86)\Notepad++\notepad++.exe"
And yet, after starting new terminal (via ConEmu), the alias is not defined (other settings like environment is set correctly).
Any tips how to set-up alias via script?
Upvotes: 2
Views: 3880
Reputation: 3390
There are two noteworthy things:
You must specify -Command "file"
as the last parameter due to the following:
If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.
Not doing so means the -NoExit
parameter gets ignored and your newly started process immediately exits, taking all your new aliases with it.
In other words, make sure to start PowerShell this way:
powershell.exe -NoExit -Command c:\workspace\script\profile.ps1
Defining aliases in a script means they are defined in the Script
scope rather than Global
scope as the profile does (read get-help about_Scopes for more info).
The trick then is to define a scope when creating the alias:
Set-Alias npp "C:\Program Files (x86)\Notepad++\notepad++.exe" -Scope Global
Now the alias should be usable as soon as the script has run.
Upvotes: 6
Reputation: 10847
Just to show easier way to edit or create your profile
notepad $profile
If the profile script was not created before - just call
new-item -itemtype file -path $profile
notepad $profile
Upvotes: 0
Reputation: 200203
That's what profiles are for. Create a file profile.ps1
in your profile directory ($env:USERPROFILE\Documents\WindowsPowerShell
) and put the alias definition there. The profile is read whenever you start a PowerShell or ISE instance.
Aliases you define in a regular script are volatile, i.e. they exist only as long as the PowerShell process running the script exists.
Upvotes: -1