Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cd in PowerShell to, cd plus ls

How I can write a function or alias a command in PowerShell so that cd can do two things:

  1. cd the given dir
  2. ls the dir content

EDIT:

What I need is this:

function ccd
{
    param($path)
    set-location $path 
    ls
}

but instead of ccd I want to write cd. When I change the function name to cd (from ccd) it changes the directory, but does not list the items in it :(. Seems that my cd function is being overridden.

like image 257
Narek Avatar asked Aug 27 '12 14:08

Narek


1 Answers

You mean something like this?

function Set-LocationWithGCI{
    param(
            $path
         )
    if(Test-Path $path){
        $path = Resolve-Path $path
        Set-Location $path
        Get-ChildItem $path
    }else{
        "Could not find path $path"
    }
}
Set-Alias cdd Set-LocationWithGCI -Force

I see that you actually want to change the built-in cd alias. To do that, you would need to remove the existing one then create the new one:

Remove-Item alias:\cd
New-Alias cd Set-LocationWithGCI
like image 184
EBGreen Avatar answered Sep 25 '22 14:09

EBGreen