Search code examples
powershell-5.0

Equivalence command basename and dirname linux in powershell


Powershell: 5.0

Hi!

How powershell get basename from file with "." in name?

And for a dir ?

  • Example:
Filename: test.txt
dir: /test01.test/
  • Desired result:
Filename: test
dir: test01

Thanks


Solution

  • That's not what basename in linux does, unless you specify the suffix:

    basename /users/js/file.txt
    file.txt
    
    basename file.txt .txt               
    file
    

    However here's a way to do what you want. It prints out the basename property of the fileinfo or dirinfo object. % is short for foreach-object.

    get-item file.txt | % basename
    file