Categories

Getting the folder name

Consider the file information from get-childitem

PS> $file = ls servicesv0.6.txt

 

Fullname gives the full path to the file

PS> $file.Fullname
C:\MyData\SkyDrive\Data\scripts\servicesv0.6.txt

 

if you use DirectoryName you get the full path to the parent directory

 

PS> $file.DirectoryName
C:\MyData\SkyDrive\Data\scripts

 

The easiest way to get the folder holding the path is like this

PS> Split-Path -Path $file.DirectoryName -Leaf
scripts

 

However on a Windows 8/PS 3 setup you can also do this

PS> $file.Directory | select -ExpandProperty Name
scripts

Leave a Reply