使用powershell获取短路径

use*_*704 3 powershell

我试图使用PowerShell中的以下代码获取短路径.对于某些文件夹,它可以工 对某些人来说,它不起作用.

$a = New-Object -ComObject Scripting.FileSystemObject 
$f = $a.GetFile("C:\Program Files\Internet Explorer") 
$f.ShortPath
Run Code Online (Sandbox Code Playgroud)

虽然文件夹可用,但我收到以下错误:

Exception calling "GetFile" with "1" argument(s): "Exception from HRESULT: 0x800A0035 (CTL_E_FILENOTFOUND)"
At C:\Misc\GetShortPath.ps1:4 char:1
+ $f = $a.GetFile("C:\Program Files\Internet Explorer")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
Run Code Online (Sandbox Code Playgroud)

有人可以请帮助

Jos*_*efZ 7

区分文件文件夹:

$a = New-Object -ComObject Scripting.FileSystemObject 
$f = $a.GetFile("C:\Program Files\Internet Explorer\iexplore.exe") 
$f.ShortPath
$f = $a.GetFolder("C:\Program Files\Internet Explorer") 
$f.ShortPath
Run Code Online (Sandbox Code Playgroud)

输出:

C:\PROGRA~1\INTERN~1\iexplore.exe
C:\PROGRA~1\INTERN~1
Run Code Online (Sandbox Code Playgroud)