Converting the FileSystemObject's ShortName Property

Definition: Returns the 8.3 version of a long filename.


This one is a little difficult in Windows PowerShell. We couldn’t come up with an easy way to do this with a cmdlet or .NET Framework object, but we did manage it with a combination of cmdlets and WMI.

We start by calling Get-WMIObject to create a WMI query that returns a reference to a file, in this case a file with the name C:\Scripts\Add_Access_Rules.ps1. Notice that you still need to follow the rules of WMI queries, which includes using two backslashes in the path, one to represent the backslash and one to “escape” the backslash so it’s treated as a backslash and not a WMI special character.

After we’ve retrieved the file, we pipe that file to the Select-Object cmdlet. We use the Select-Object cmdlet to retrieve only the EightDotThreeFileName property for that file. We assign this property to the variable $a.

Next we call the Split-Path cmdlet on $a.EightDotThreeFileName, which contains the full path to the file with the filename in 8.3 format. Because we want only the filename and not the full path, we use the -leaf parameter of Split-Path to return only the file name.

$a = (Get-WMIObject -query "Select * From CIM_DataFile Where Name = 'C:\\Scripts\\Add_Access_Rule.ps1'" | Select-Object EightDotThreeFileName)
Split-Path $a.EightDotThreeFileName -leaf

Note that you’ll receive an error message if the given file doesn’t exist.

See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page