Converting the FileSystemObject's ShortPath Property
Definition: Returns the full path in 8.3 format.
ShortPath
We haven’t been able to find an equivalent to this in PowerShell or the .NET Framework, although honestly we’re pretty sure this won’t be a problem for most people. But if you really need to get the 8.3 format of a path in PowerShell, here are a couple things you can try:
This example will return the 8.3 format of the file name in the path, but it won’t return the 8.3 format of the folders in the path.
$a = (Get-WMIObject -query "Select * From CIM_DataFile Where Name = 'C:\\Scripts\\Employees.xls'" | Select-Object EightDotThreeFileName)
We’ve used the Get-WMIObject cmdlet to create a WMI query that retrieves a file named - in this example - C:\Scripts\Employees.xls. We’ve piped this object to the Select-Object cmdlet to retrieve only the EightDotThreeFileName property. That will return this:
EightDotThreeFileName
---------------------
c:\scripts\employ~1.xls
However, if you perform the same command on a path with a long folder name in it, the folder name will remain in long format:
$a = (Get-WMIObject -query "Select * From CIM_DataFile Where Name = 'C:\\Scripttest\\Employees.xls'" | Select-Object EightDotThreeFileName)
EightDotThreeFileName
---------------------
c:\scripttest\employ~1.xls
This will work on a folder (not just files) by using the Win32_Directory WMI class, but it only works for the last folder in the path. Here’s an example:
$a = (Get-WMIObject -query "Select * From Win32_Directory Where Name = 'C:\\Scripttest\\morescripts'" | Select-Object EightDotThreeFileName)
EightDotThreeFileName
---------------------
c:\scripttest\moresc~1
Notice how we were able to retrieve the 8.3 format of the morescripts folder, but not the scripttest folder.
If you really need the whole thing in 8.3 format, just use the FileSystemObject:
$a = New-Object -ComObject Scripting.FileSystemObject
$f = $a.GetFile("C:\Scripttest\LongFileNameTest.txt")
$f.ShortPath
Here we’ve used the New-Object cmdlet to create the COM object Scripting.FileSystem. We then call GetFile to retrieve the file we’re looking for. We then simply display the ShortPath property of that file:
C:\SCRIPT~2\LONGFI~1.TXT
And yes, that was a lot of explanation for a method you probably won’t find much need for.
See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page