Search code examples
windowspowershellpowershell-3.0

Get-ChildItem equivalent of DIR /X


How do I show a directory listing in 8.3 notation in PowerShell?


Solution

  • You can use WMI:

    Get-ChildItem | ForEach-Object{
        $class  = if($_.PSIsContainer) {"Win32_Directory"} else {"CIM_DataFile"}
        Get-WMIObject $class -Filter "Name = '$($_.FullName -replace '\\','\\')'" | Select-Object -ExpandProperty EightDotThreeFileName
    }
    

    Or the Scripting.FileSystemObject com object:

    $fso = New-Object -ComObject Scripting.FileSystemObject
    
    Get-ChildItem | ForEach-Object{
    
        if($_.PSIsContainer) 
        {
            $fso.GetFolder($_.FullName).ShortPath
        }
        else 
        {
            $fso.GetFile($_.FullName).ShortPath
        }    
    }