Question:
I am trying to get shortpath using the below code in powershell. For some folders it works. For some it does not work.
1 2 3 4 |
$a = New-Object -ComObject Scripting.FileSystemObject $f = $a.GetFile("C:\Program Files\Internet Explorer") $f.ShortPath |
I get the below error although the folders are available :
1 2 3 4 5 6 7 |
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 |
Can someone please help
Answer:
Distinguish files and folders:
1 2 3 4 5 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 |
Output:
1 2 3 |
C:\PROGRA~1\INTERN~1\iexplore.exe C:\PROGRA~1\INTERN~1 |