If you’re reading this blog, you know what I am.  I’m a tech.  I a tech who deeply loves what Powershell does to help enable me on my job.  Not because Microsoft says so, but because I’ve gone from one side of the Coin doing pure GUI and a pile of Command line functions, to one Interface. 

So the thing I was playing with Today was split(). I was trying to figure out how to separate a FILENAME from a DIRECTORY name in a string that just had both.  My Example?

The $PROFILE variable, The Object that contains the pathname and filename of your personal Powershell Profile. Typically it looks like


So how do you pull just the filename out of that?  Well actually if you knew that the filename was always a certain size and ALWAYS at the end you could use a substring() and say something like “Give me the last 20 letters”.  But a better way would to be find out how to pull out “all the information after the end of the Directory Name”

Which is where split() comes in.  If you run


Which says “Everytime you see a "/” in that line, break that into another chunk and store the results in the $PIECES object.  This of course is a name I completely made up, you could call it $PICTURESOFLILLY if you so liked 😉

What $PIECES will look like will be


Now here is where it starts to get easy.   The last Line will ALWAYS be the filename.  The preceding bits will always be the directory since the SPLIT() breaks it down in the ORDER it SEES it.  If you run a GET-MEMBER against $PIECES you’ll also notice there is a Property called Count.  Count is ALWAYS the number of members in that array.  Always.


Which is 6. So since I now have a simple list, and a way to know how many pieces are in that list, I can VERY easily say “Show me the last piece in the list which I already know is the Filename”


We say –1 in the Count because although you an *I* count from from 1 to 6, the computer starts at 0 for it’s numbering of the array.  So there ARE 6 pieces by the count but they are numbered from 0 to 5

So if you had to make all of this into a nice little Function here is how easy it is!

function global:GET-FILENAME ($FullPathName) {

return $FILENAME


If you like to return the Directory as well?  Just use the trim() Method we discovered yesterday and “Trim” the filename from the string! You can add that in one line and return both pieces like this.

function global:GET-SPLITFILENAME ($FullPathName) {




Now wasn’t that easy?

Powershell.  The Shell of Champions

The Energized Tech