使用powershell

时间:2016-06-19 07:02:37

标签: powershell

我正在尝试使用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

有人可以帮忙吗

1 个答案:

答案 0 :(得分:6)

区分文件文件夹

$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