Path to files on start menu–Part 2

Last time we had got the file names for most of the files but anything in special folders such as the desktop wasn’t displaying properly.

We can fix that

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029

$rd = New-Object -ComObject Shell.Application
Get-ChildItem -Path $rd.Namespace(0x8).Self.Path | 
where {-not $_.PSIsContainer} | 
foreach
 {
 
if (Test-Path $_.
Fullname){
  
$name = ($_.Name).Replace(".lnk",""
)
  
$ln = $name.
length
 
  
$txt = (Select-String -InputObject (Get-Content -Path $_.Fullname ) -Pattern ":\" -SimpleMatch).
ToString()
  
$i = $txt.LastIndexOf(":\"
)
  
$txt2 = $txt.SubString($i-1
)
  
  
if ($txt2.Startswith("C:\Users\"
) ) {
    
$txt2 = $txt2.Remove(9, 22
)
    
$txt2 = $txt2.Replace("`\`\`\$env:COMPUTERNAME",""
)
    
$txt2 = $txt2.Replace("$env:USERNAME",""
)
    
$txt2 = $txt2.Replace("Users\Users","Users"
)
    
$txt2 = $txt2.Replace("$(([char]0).ToString())",""
)
   } 
  
  
$j = $txt2.IndexOf("$name") + $ln
 
  
 
# $txt2.SubString(0, 100)
   $filepath = $txt2.SubString(0, $j
)
  
  
$filepath
 }
    
}

 

Once we have the string starting with the path to the file we can test to see if it starts with “c:\users”. If it does we strip out the data we don’t want and then find the file name in the string. Work out the end point of the name and substring down to that.

The replaces could be concatenated but this makes it easier to read

The part of the original string that is of interest can be displayed if required

2 Responses to Path to files on start menu–Part 2

  • Hi Richard,

    Here’s a more robust way:

    $rd = New-Object -ComObject Shell.Application
    $Shell=New-Object -ComObject WScript.Shell

    Get-ChildItem -Path $rd.Namespace(0x8).Self.Path |
    where {-not $_.PSIsContainer} |
    where {Test-Path $_.Fullname} | Foreach {
    $Shell.CreateShortcut($_.FullName).TargetPath
    }

    Cheers,
    Chris

  • Hi again Richard,

    In fact, this is even better…

    Cheers, Chris

    $RecentFolder=(New-Object -ComObject Shell.Application).Namespace(8)
    $Shell=New-Object -ComObject WScript.Shell
    $RecentFolder.Items()|Foreach {$Shell.CreateShortcut($_.Path).TargetPath}

Leave a Reply