Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell configuration with an rc-like file

What script and where I should write in order to define:

  1. alias for ll="ls -l"
  2. alias/function cd = "original cd; ll"

So, my question has to parts where is the rc files of the Power Shell on Windows 7 and how to alias ll to ls -l and cd to cd; ll?

like image 884
Narek Avatar asked Aug 27 '12 13:08

Narek


People also ask

Where is PowerShell config file?

A powershell. config. json file in the $PSHOME directory defines the configuration for all PowerShell sessions running from that PowerShell installation.

How do I run DSC configuration?

For a DSC configuration to be applied to a node, it must first be compiled into a MOF file. Running the configuration, like a function, will compile one . mof file for every Node defined by the Node block. In order to run the configuration, you need to dot source your HelloWorld.

How do I navigate to C drive in PowerShell?

The Windows PowerShell prompt opens by default at the root of your user folder. Change to the root of C:\ by entering cd c:\ inside the Windows PowerShell prompt.


1 Answers

Create a file in the place where the power shell points when you type $profile and press enter, if it does not exist. (For more info look here.)

Also I have found a lots of good examples next to the powershell.exe in my system there is an example folder, where there is a file named profile.ps1 with the following code:

set-alias cat        get-content
set-alias cd         set-location
set-alias clear      clear-host
set-alias cp         copy-item
set-alias h          get-history
set-alias history    get-history
set-alias kill       stop-process
set-alias lp         out-printer
set-alias ls         get-childitem
set-alias mount      new-mshdrive
set-alias mv         move-item
set-alias popd       pop-location
set-alias ps         get-process
set-alias pushd      push-location
set-alias pwd        get-location
set-alias r          invoke-history
set-alias rm         remove-item
set-alias rmdir      remove-item
set-alias echo       write-output

set-alias cls        clear-host
set-alias chdir      set-location
set-alias copy       copy-item
set-alias del        remove-item
set-alias dir        get-childitem
set-alias erase      remove-item
set-alias move       move-item
set-alias rd         remove-item
set-alias ren        rename-item
set-alias set        set-variable
set-alias type       get-content

function help
{
    get-help $args[0] | out-host -paging
}

function man
{
    get-help $args[0] | out-host -paging
}

function mkdir
{
    new-item -type directory -path $args
}

function md
{
    new-item -type directory -path $args
}

function prompt
{
    "PS " + $(get-location) + "> "
}

& {
    for ($i = 0; $i -lt 26; $i++) 
    { 
        $funcname = ([System.Char]($i+65)) + ':'
        $str = "function global:$funcname { set-location $funcname } " 
        invoke-expression $str 
    }
}

Also take into account the following problem. You may have the following error while executing the file located in $profile:

"Microsoft.PowerShell_profile.ps" cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

Solution: Check the current execution-policy

PS C:\Windows\System32> Get-ExecutionPolicy
Restricted
PS C:\Windows\System32>

To change the execution policy to allow PowerShell to execute scripts from local files, run the following command:

PS C:\Windows\System32> Set-Executionpolicy RemoteSigned -Scope CurrentUser
like image 147
Narek Avatar answered Sep 30 '22 21:09

Narek