Converting File Paths to 8.3 (Part 1)

by Oct 15, 2020

Many years ago, file and folder names had a maximum of 8 characters, and these short path names still exist. They can even still be useful: short path names never contain spaces and other special characters and therefore never need to be quoted or escaped. Short paths can also be helpful when paths get very long.

Yet how can you find out the short path name for a default long path name? One way is to use an old COM component that was used by the Windows scripting host:

# take any path
# in this example, I am taking the path where Powershell 7 is installed
# this requires that PowerShell 7 in fact is installed.
# You can use any other path as well
$path = (Get-Command -Name pwsh).Source

"Long path: $path"

# convert it to 8.3 short name
$shortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFile($path).ShortPath 
"Short path: $shortPath"

The result looks similar to this:

Long path: C:\Program Files\PowerShell\7\pwsh.exe
Short path: C:\PROGRA~1\POWERS~1\7\pwsh.exe 

Twitter This Tip! ReTweet this Tip!