Powershell – Create a folder from a file name, then place that file in the folder

Question:

I have a list of files say…

etc. etc. etc. for about 900 more files

What I am needing to do is create a folder based on the characters before the first underscore, but to not repeat it since there are multiple files.

So in the example above, I would only want two folders named T123 and T456.

Then I would need the script to place the appropriate files in the folder.

I had found some codes in this thread, but they don’t exactly do what I’m looking for.

https://superuser.com/questions/306890/windows-batch-script-to-create-folder-for-each-file-in-a-directory-name-it-tha

Any help?

Answer:

The easiest way here would be to group the files by the first part, which will then become the directory name. In typical PowerShell pipeline manner this is fairly succinct:

Source:

Powershell – Create a folder from a file name, then place that file in the folder by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply