Get-ChildItem equivalent of DIR /X

2019-07-02 01:56发布

问题:

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

回答1:

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
    }    
}


回答2:

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


回答3:

You attract my attention, this is not the full answer but my way to help you :

First : have a look to how to Control 8dot3 naming in Windows 2008 and Windows 7.

Second : here is a solution to Convert path to Dos 8.3 notation using C# that you can modify or use as is in PowerShell.