How to create permanent PowerShell Aliases
I want to create an alias
of a cmdlet
that doesn't expire after I close the current session of Powershell, let's say I have this alias :
C:\Users\Aymen> New-Alias Goto Set-Location
This perfectly creates the Goto
alias, but I want to use it even after I close the current session, how can I achieve that.
Note:
The PowerShell Help system suggests that I can export the aliases I create, and import them next time I open a new session, actually that's not really what I'm looking, for, is there a direct clear way to keep having a alias after I create it through different sessions
UPDATED - January 2021
It's possible to store in a profile.ps1
file any PowerShell code to be executed each time PowerShell starts. There are at least 6 different paths where to store the code depending on which user has to execute it. We will consider only 2 of them: the "all users" and the "only your user" paths (follow the previous link for further options).
To answer your question, you only have to create a profile.ps1
file containing the code you want to be executed, that is:
New-Alias Goto Set-Location
and save it in the proper path:
-
"$Home\Documents"
(usuallyC:\Users\<yourname>\Documents
): only your user will execute the code. This is the recommended location You can quickly find your profile location by runningecho $profile
in PowerShell -
$PsHome
(C:\Windows\System32\WindowsPowerShell\v1.0
): every user will execute this code
IMPORTANT: remember you need to restart your PowerShell instances to apply the changes.
TIPS
-
If both paths contain a
profile.ps1
file, the all-users one is executed first, then the user-specific one. This means the user-specific commands will overwrite variables in case of duplicates or conflicts. -
Always put the code in the user-specific profile if there is no need to extend its execution to every user. This is safer because you don't pollute other users' space (usually, you don't want to do that).
Another advantage is that you don't need administrator rights to add the file to your user-space (you do for anything in C:\Windows\System32). -
If you really need to execute the profile code for every user, mind that the
$PsHome
path is different for 32bit and 64bit instances of PowerShell. You should consider both environments if you want to always execute the profile code.The paths are:
-
C:\Windows\System32\WindowsPowerShell\v1.0
for the 64bit environment -
C:\Windows\SysWow64\WindowsPowerShell\v1.0
for the 32bit one (Yeah I know, the folder naming is counterintuitive, but it's correct).
-
It's not a good idea to add this kind of thing directly to your $env:WINDIR
powershell folders, unless you truly want your alias to be global.
The recommended way is to add it to your personal profile:
cd $env:USERPROFILE\Documents
md WindowsPowerShell -ErrorAction SilentlyContinue
cd WindowsPowerShell
New-Item Microsoft.PowerShell_profile.ps1 -ItemType "file" -ErrorAction SilentlyContinue
powershell_ise.exe .\Microsoft.PowerShell_profile.ps1
Now add your alias to the Microsoft.PowerShell_profile.ps1 file that is now opened:
function Do-ActualThing {
# do actual thing
}
Set-Alias MyAlias Do-ActualThing
Then save it, and refresh the current session with:
. $profile
Note: Just in case, if you get permission issue like
CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
Try the below command and refresh the session again.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
2018, Windows 10
You can link to any file or directory with the help of a simple PowerShell script.
Writing a file shortcut script
Open Windows PowerShell ISE. In the script pane write:
New-Alias ${shortcutName} ${fullFileLocation}
Then head to the command-line pane. Find your PowerShell user profile address with echo $profile
. Save the script in this address.
The script in PowerShell's profile address will run each time you open powershell. The shortcut should work with every new PowerShell window.
Writing a directory shortcut script
It requires another line in our script.
function ${nameOfFunction} {set-location ${directory_location}}
New-Alias ${shortcut} ${nameOfFunction}
The rest is exactly the same.
Enable PowerShell Scripts
By default PowerShell scripts are blocked. To enable them, open settings -> Update & Security -> For developers. Select Developer Mode (might require restart). .
Scroll down to the PowerShell section, tick the "Change execution policy ..." option, and apply.
Open a Windows PowerShell window and type:
notepad $profile
Then create a function, such as:
function goSomewhereThenOpenGoogleThenDeleteSomething {
cd C:\Users\
Start-Process -FilePath "http://www.google.com"
rm fileName.txt
}
Then type this under the function name:
Set-Alias google goSomewhereThenOpenGoogleThenDeleteSomething
Now you can type the word "google" into Windows PowerShell and have it execute the code within your function!
Just to add to this list of possible locations...
This didn't work for me:
\Users\{ME}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
However this did:
\Users\{ME}\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
If you don't have a profile or you're looking to set one up, run the following command, it will create the folder/files necessary and even tell you where it lives!
New-Item -path $profile -type file -force