0

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?

kamokoba
  • 413
  • 6
  • 14

2 Answers2

1

You put a function definition into the script. You must now call/execute this function somewhere. Your function fp does the same thing as cd C:\Users\user\puppet\modules\fp\files\configs when it is called.

If you want your function to be available at the command prompt, you have to prefix its name with global:, as described here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#function-scope

adsc
  • 307
  • 3
  • 9
  • could you specify how should I invoke the function that is stored in a script? just type "fp", "fp()", "script.ps1 fp" ? none of these works. – kamokoba Mar 03 '20 at 15:43
  • @kamokoba The function definition is only available within the defined script on default. You can define the scope of a function like described here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#function-scope – adsc Mar 03 '20 at 16:17
  • ok, so i added "global:" to my function, now it's 'function global:fp { set-location "C:\Users\user\puppet\modules\fp\files\configs" }'. Restarted PS session and tried to run the script again - nothing happens. Anything crucial that should be present in PATH? – kamokoba Mar 04 '20 at 13:14
  • 1
    @kamokoba Ok, let's try a different approach. In your Documents folder, create a folder called WindowsPowerShell, and in this folder, create a file called Microsoft.PowerShell_profile.ps1 and put your function definition in there. Then restart PowerShell and try to execute your function by simply typing its name in the shell. – adsc Mar 04 '20 at 15:15
  • thank you, it works now. It works even without "global" keyword – kamokoba May 01 '20 at 16:58
1

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
  • I've already added the function to Microsoft.PowerShell_profile.ps1 file - still no use – kamokoba Mar 03 '20 at 15:48
  • 1
    @kamokoba: The file that is returned when you enter `$PROFILE.CurrentUserAllHosts` in your PowerShell terminal? Is your function displayed when you execute `Get-Content $PROFILE.CurrentUserAllHosts`? Also: Have you restarted your PowerShell session (simply closing the terminal and reopening it should be enough) since you added that into the file? For me that file is `$HOME\Documents\WindowsPowerShell\profile.ps1`, not `Microsoft.PowerShell_profile.ps1`, although that should also work in most cases. – CGundlach Mar 03 '20 at 16:10
  • 1
    @kamokoba I have added exact commands to add your function in suitable profile. – stackoverflowusrone Mar 06 '20 at 18:09