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

This entry was posted in Powershell, Scripting Games. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s