Get-ChildItem equivalent of DIR /X

坚强是说给别人听的谎言 提交于 2019-12-23 14:22:10

问题


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.




回答4:


You could run cmd...

cmd /c dir /x

Note that get-childitem -filter matches on the short version of the filenames too!

get-childitem -filter *~1*


来源:https://stackoverflow.com/questions/16995359/get-childitem-equivalent-of-dir-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!