我正在尝试在 powershell 中使用以下代码获取短路径。对于某些文件夹,它有效。对于某些人来说,它不起作用。

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

尽管文件夹可用,但我收到以下错误:
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

有人可以帮忙吗

最佳答案

区分文件和文件夹:

$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

输出:
C:\PROGRA~1\INTERN~1\iexplore.exe
C:\PROGRA~1\INTERN~1

关于powershell - 使用powershell获取shortpath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37904757/

10-09 00:37