1

I'm basically looking for something like ls -alt, but in Powershell (Windows 11). Currently I'm using

gci -fo | sort LastWriteTime -Descending

but it's a bit of a doozy, and definitely not easy to remember. Isn't there anything shorter, which achieves the same?

EDIT: the proposed solution using

Function ls-alt {Invoke-Expression "gci $Args -Force"} # | sort LastWriteTime -Descending}

doesn't work. Proof:

  1. create an empty dir with mkdir test

  2. cd test

  3. notepad foo, write some string (I wrote string), save and close

  4. copy some older file into test (I used an existing .gitconfig, but I think any older file would work)

  5. PS C:\Users\xxx\test> gci | sort LastWriteTime -Descending

     Directory: C:\Users\xxx\test
    

    Mode LastWriteTime Length Name


    -a---- 3/18/2024 10:04 PM 6 foo.txt -a---- 8/31/2023 9:13 AM 66 .gitconfig

PS C:\Users\xxx\test> ls-alt


    Directory: C:\Users\xxx\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         8/31/2023   9:13 AM             66 .gitconfig
-a----         3/18/2024  10:04 PM              6 foo.txt

The proposed solution doesn't correctly order files by LastWriteTime. The content of the profile file is as expected:

notepad $PROFILE
Function ls-alt {Invoke-Expression "gci $Args -Force"} # | sort LastWriteTime -Descending}
3
  • 1
    Why not define it as a function in your profile? Then you can give it any name you want, i.e. Function ls-alt {gci -force | sort LastWriteTime -Descending}. Commented Mar 16 at 2:32
  • 1
    ...and if you want it to take parameters, esaentially acting like a customized gci, use: Function ls-alt {Invoke-expression "gci $Args -Force"} # | sort LastWriteTime -Descending}. That will allow ls-alt -s (Recurse), ls-alt $HOME, etc. Commented Mar 16 at 2:57
  • 1
    @KeithMiller You should convert your comments into an answer.
    – DavidPostill
    Commented Mar 16 at 8:55

1 Answer 1

1

If you want a customized version of a Cmdlet or a series of actions accessible with minimal typing, define it as a function in your PowerShell profile -- then it will be available in every console session you run.

A simple version would wrap your example verbatim:

Function ls-alt {gci -force | sort LastWriteTime -Descending}

But this limits its to the current directory with none of the familiar options of filtering, recursion, etc.

To have your customizations as the defaults and retain the use of positional parameters, named parameters, and aliases native to Get-ChildItem, you can define it using Invoke-Expression and the $Args automatic variable:

Function ls-alt {Invoke-Expression "gci $Args -Force" | sort LastWriteTime -Descending}

That will allow ls-alt -s (Recurse), ls-alt $HOME, etc.

9
  • nice! Where do I write this instruction? In other words, where is the PowerShell profile? Do I need to be an admin to edit it?
    – DeltaIV
    Commented Mar 18 at 13:18
  • @DeltaIV: no, you don't need to be an advent. In PowerShell type notepad $PROFILE. If you don't have a profile, you'll be creating it by adding this function and just save the file. Launch a new console and your function it should be available Commented Mar 18 at 18:17
  • It doesn't work. Please have a look at my edited question.
    – DeltaIV
    Commented Mar 18 at 21:12
  • I just looked at my answer a pound sign crept into the code and commented out the sort in the function definition try it with the fixed version Commented Mar 20 at 6:19
  • Should have told you to test it in the console first -- you would get the continuation prompt waiting for the closing bracket... Commented Mar 20 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .