如何在PowerShell中以8.3表示法显示目录列表?
如何在PowerShell中以8.3表示法显示目录列表?
Get-ChildItem | ForEach-Object{
$class = if($_.PSIsContainer) {"Win32_Directory"} else {"CIM_DataFile"}
Get-WMIObject $class -Filter "Name = '$($_.FullName -replace '\\','\\')'" | Select-Object -ExpandProperty EightDotThreeFileName
}
$fso = New-Object -ComObject Scripting.FileSystemObject
Get-ChildItem | ForEach-Object{
if($_.PSIsContainer)
{
$fso.GetFolder($_.FullName).ShortPath
}
else
{
$fso.GetFile($_.FullName).ShortPath
}
}
Scripting.FileSystemObject
方法更快。至于WMI/CIM方法:考虑到'
是文件名中的合法字符,最好在过滤器内使用\
"...`",如下所示(还将
Get-WmiObject替换为
Get-CimInstance(PSv3+)):
Get-ChildItem | ForEach-Object { $class = if ($.PSIsContainer) { "Win32_Directory" } else { "CIM_DataFile" } Get-CimInstance $class -Filter "Name = `"$($.FullName -replace '\','\')`"" | Select-Object -ExpandProperty EightDotThreeFileName }` - mklement0Get-ShortPath
cmdlet进行以下操作:dir | Get-ShortPath
或者
dir | Get-ShortPath | select -expa shortpath
choco install pscx -y
- Cameron Taggart(Get-ShortPath "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10586.0\um\x86").ShortPath
这样的方式来获取 C:\PROGRA~2\WI3CF2~1\10\Lib\100105~1.0\um\x86
。 - Cameron Taggart在jpblanc的答案基础上,这里提供一种方法,可以通过调用Win32 GetShortPathName() API来缩短整个路径:
function Get-ShortPathName
{
Param([string] $path)
$MethodDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)]
public static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int cbShortName);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
$shortPath = New-Object System.Text.StringBuilder(500)
$retVal = $Kernel32::GetShortPathName($path, $shortPath, $shortPath.Capacity)
return $shortPath.ToString()
}
你可以运行 cmd...
cmd /c dir /x
get-childitem -filter *~1*
你引起了我的注意,这不是完整的答案,而是我帮助你的方式:
首先:看一下如何在Windows 2008和Windows 7中控制8dot3命名。
其次:这里有一个使用C#将路径转换为Dos 8.3符号的解决方案,你可以修改或直接在PowerShell中使用。