Create formatted list of files using split string – Powershell

Got a request today to build a csv file that listed out some files in a folder along with a few other bits of information. The headers from left to right are: parentId($id), Description($size), Name($name), Body($unc) The first few items were very simple, they needed:

1) Full path

2) File name

3) File size

The fourth item was a bit trickier. There are two formats to the files one with an ID and ones without (with: INV-100-001267-000aaaa-0067000000aaaaa.pdf without: INV-100-001272-000bbbb-.pdf). Basically files that do not have an ID just have a null value in the exported list. The first issue was actually parsing that id out of the file name:


Ok so this works, but we get unwanted results when the ID does not exist as we are losing an index in our array which causes the “pdf” element to be saved in the $id variable. Im sure there are other ways around this but here is how I solved it:


What the above code does, is check the array for 6 indexes (0-5), if there are 6 it uses index 4 to pull the ID. If there is not a 6th index it will null the field out.

Here is the complete solution:


And here is an example of the csv file that is created:



Hope that helps you out!


Source for Split String:

0 comments… add one

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.