I want to implement some alias-like command that could allow me to navigate through a long directory path by typing less.
In this answer I learned that it's not possible to create aliases for commands like "cd c:\some\dir
", but it's possible to create a function, save it into a script and then run it.
So I created a script, saved it in directory C:\ps_scripts, made sure this directory is listed in PATH, then opened PS from C:\ps_scripts and tried to issue command .\script.ps1 and nothing happened - no error, no output, no navigation to the desired directory path. script.ps1 contains the following code:
function fp { set-location "C:\Users\user\puppet\modules\fp\files\configs" }
Then I searched for solution here and tried to run the script by appending powershell -noexit "&
as advised in the accepted answer, but got error term '$' is not recognized
.
And my execution policy is set to RemoteSigned.
What could be the issue?
You should use appropriate PowerShell profiles for such things, like functions, commands and settings.
You can get the profile path for applicable scope - User (current or all) and host scope (current host or all hosts).
E.g. To get the profile path for current user for all hosts (Windows PowerShell console, ISE), you can type this in PowerShell -
Write-Output $PROFILE.CurrentUserAllHosts
which will tell you the path of profile file that will be used for the scope.
Create a file with same path and name as per output of the command and put your function in that file. This will be automatically loaded for any PowerShell session by current user, and you can use that function without running the script prior manually. This behaves similar to .bashrc file in Linux.
Commands to setup your function in the profile
Run these exact commands in your PowerShell and then restart the PowerShell, it will start working then, and you can use fp
after this to call that function from that user profile.
New-Item -Path $PROFILE.CurrentUserAllHosts -Force
Add-Content -Path $PROFILE.CurrentUserAllHosts -Value 'function fp { set-location "C:\Users\user\puppet\modules\fp\files\configs" }'
exit