You or whomever is creating these files, need to rethink the file naming scheme and pad leading zeros to them when they are created vs forcing you to deal with this after. It just leads to a bunch of unnecessary string gymnastics. Just as coding / scripting has standards that should be followed, file naming is part of this as well.
The sorting you are seeing, is not a PS issue. If you’d open this same list in MS Excel, you’d get the same thing. Sorting is always by character representation. 1 and 01, etc., are different of course. So, to sort, the leading part must have the same number of characters. In your case. 001…014, for example or whatever the max number span might be.
So, either these files as they are created are already properly formed or you are going to have to do a far more cumbersome effort, inline code (lead zero padding using padleft for the string, or using the custom format switch while convert the number string to an integer ), or renaming the files on disk to get what you are after.
Also what you posted, has leading spaces. If that is the case for real on the file system, vs a bad copy / paste here, you’ll have to deal with that as well on that string using trim(). Again, common naming taxonomy would make this moot.
IMHO, I’d just get the owner to rename the files on creation properly or get permission to rename them if they chose not to. It will make things far easier on you to use normal filesystem cmdlets to act on the files.
As far as the string gymnastics, this is the sort of stuff you are getting yourself into if you do not address this at its root cause.
Assuming from your posted code the way you are getting this if not, it also leading spaces So, you’ll have to deal with that as well.
# Trim leading spaces
Clear-Host
((Get-Content -Path '.\MusicData.txt').Trim()) | Sort-Object
# Results
1 incident.mp4
10 Action.mp4
11 Isolation.MP4
12 Interogation.mp4
13 Decision Making.MP4
14 Final result.mp4
2 report.mp4
3 filing.mp4
4 investigation.mp4
5 report collection.mp4
6 examine.mp4
7 report.mp4
8 Rep Analysis.mp4
9 case study.mp4
Then you can use .padLeft or custom formatting to add leading zero, before you later sort.
# Split on the first space and pad the number line
Clear-Host
((Get-Content -Path '.\MusicData.txt').Trim() -split " ",2) |
%{If ($_ -match '^\d'){"{0:D4}" -f [int]$_}} | Sort-Object
# Results
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
Now, the string gymnastics, which can be avoided if a standard naming construct is used, or you just rename them before doing filesystem stuff with them.
# Putting it all together
# Collect the files from disk. Note I am using a filename list since of course I don't have these and no real reason to create them
# Trim any leading spaces
# Parse the numbers in the string and pad with 4 zeros (using custom formatting vs padleft) so, this supports up to 0 - 9999 files
# Parse the string again, the keep the remaining text on the same line.
Clear-Host
($MediaFiles = ForEach($Line in ((Get-Content -Path '.\MusicData.txt').Trim()))
{$Line -replace '^\d*',("{0:D4}" -f ("{0:D4}" -f ([int]($Line -split ' ',2)[0])))}) | Sort-Object
# Results
0001 incident.mp4
0002 report.mp4
0003 filing.mp4
0004 investigation.mp4
0005 report collection.mp4
0006 examine.mp4
0007 report.mp4
0008 Rep Analysis.mp4
0009 case study.mp4
0010 Action.mp4
0011 Isolation.MP4
0012 Interogation.mp4
0013 Decision Making.MP4
0014 Final result.mp4
There may be far more elegant ways in dealing with this. Yet, my original opinion, still is prudent. Save yourself the unnecessary headaches as you are experiencing now based on this thread thus far.
Anyway HTH