locked
How set alias or a function to "cd C:\some\path" ? RRS feed

  • Question

  • I want to set an alias to a command "cd \some\very\long\dir\path\", so that instead of writing the entire path, I could simply type two letters.

    I tried:

    Set-Alias -Name fp -Value cd C:\Users\some\dir\

    And got error "A positional parameter cannot be found that accepts argument"

    Then I tried:

    function fp { set-location "cd C:\Users\some\dir\" }

    and got error "fp : The term ... is not recognized as the name of a cmdlet..."

    How could I set an alias so that when I type "fp" and hit enter, I am being navigated to C:\Users\some\dir\ ?

    EDIT: I also created "profile.ps1" (containing the function) in "C:\Users\User" and restarted PS instance, but still no luck

    Thanks


    • Edited by Kamokoba Tuesday, March 3, 2020 9:18 AM
    Tuesday, March 3, 2020 8:59 AM

All replies

  • An alias cannot contain parameters.   You can only do this by write a function with a two letter name.


    \_(ツ)_/

    Tuesday, March 3, 2020 9:59 AM
  • as mentioned in my problem description, apart from attempting create an alias, I also tried to create a function and provided concrete examples of my attempts
    Tuesday, March 3, 2020 10:12 AM
  • You created a script file. That is not a function.  You can only run a script file by specifying its path and full name.  YOu need to place teh function in your profile and not just in a file named "profile".

    help about_functions

    help about_profiles


    \_(ツ)_/

    Tuesday, March 3, 2020 10:17 AM
  • i pleaced the function in Microsoft.PowerShell_profile.ps1 and ran the script from the current directory and nothing happens at all.

    Also, those canned help links have been expired long ago:

    PS C:\ps_scripts> help about_functions

    Get-Help : Get-Help could not find about_functions in a help file in this session...

    Tuesday, March 3, 2020 2:35 PM
  • Hello

    function fp { set-location "cd C:\Users\some\dir\" }

    You used Set-Location AND cd, which doesn't making sense. I tried this

    Function FP {Set-Location C:\users\}

    And it did work on my side.

    PS C:\Users\steven> Function FP {Set-Location C:\users\}
    PS C:\Users\steven> cd
    PS C:\
    PS C:\> FP
    PS C:\users>
    Also, in Set-Alias page (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-alias?view=powershell-7), it is stated: An alias that is created or changed by Set-Alias is not permanent and is only available during the current PowerShell session. When the PowerShell session is closed, the alias is removed.

    Tuesday, March 3, 2020 3:05 PM
  • i pleaced the function in Microsoft.PowerShell_profile.ps1 and ran the script from the current directory and nothing happens at all.

    Also, those canned help links have been expired long ago:

    PS C:\ps_scripts> help about_functions

    Get-Help : Get-Help could not find about_functions in a help file in this session...

    What help links.  That help is in PowerShell.   I tis every version of PowerShell.  Are you sure you are running PowerShell in Windows 10?

    You need to start by learning PowerShell.  We cannot help you if you do not know the basics.


    \_(ツ)_/

    Tuesday, March 3, 2020 3:19 PM
  • I suspect that you didn't put the function in your profiles.  It has to go in the profile.

    If you are trying to use PowerShell in a remote constrained session then most commands won't work and ther will be no profiles for you to change.

    This is another reason why you must first learn basic PowerShell.  

    Here is a free eBook that will teach you basic PowerShell: Windows PowerShell™ 4: TFM


    \_(ツ)_/

    Tuesday, March 3, 2020 3:24 PM