Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem equivalent of DIR /X

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

like image 552
Old Geezer Avatar asked Jun 08 '13 02:06

Old Geezer


2 Answers

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
    }    
}
like image 97
Shay Levy Avatar answered Sep 21 '22 07:09

Shay Levy


If you install the PSCX module you have the Get-ShortPath cmdlet and you can do:

dir | Get-ShortPath

or

 dir | Get-ShortPath  | select -expa shortpath
like image 35
CB. Avatar answered Sep 21 '22 07:09

CB.