This post provides a simple method of enumerating shortcuts and retrieving their properties, either using PowerShell or VBScript. There is nothing particularly clever here, but I had a need a while back to use the enumeration, and I wrote the simple powershell equivalent to see how easy it would be. Note that I couldn't easily see a managed code .Net method to process shortcuts, so I fell back on the wscript COM object. More than likely there is a better method (get it? :) than this.
Run the scripts:
powershell . .\ReadShortcut.ps1 -p \\%server%\%share%\folder\path
powershell . .\ReadShortcut.ps1 -f \\%server%\%share%\shortcut.lnk
cscript //nologo "ReadShortcut.vbs" \\%server%\%share%\folder\path
cscript //nologo "ReadShortcut.vbs" \\%server%\%share%\shortcut.lnk
#
param(
[string] $path = "",
[string] $file = ""
)
$WshShell = new-object -comobject "WScript.Shell" # Instantiate the wscript.shell COM object
if ($path -ne "") {
$shortcuts = get-childitem -path $path -filter "*.lnk" -rec # Find all .lnk files, recursing in to subdirectories
$shortcuts | foreach-object {$WshShell.CreateShortcut($_.FullName) } # For each file, pass the fullname to the COM object to open the shortcut and enumerate the properties
} elseif ($file -ne "") {
$shortcut = get-item -path $file # Get the single file
if ($shortcut -ne $null) { $WshShell.CreateShortcut($shortcut) } # If exists, read the shortcut properties
} else {
write-output "No arguments specified, please use either -p to specify a path or -f for a specific .lnk file"
}
#-------#
'ReadShortcut.vbs
' Read a shortcut or a top-level directory of shortcuts and write the properties to stdout
If WScript.Arguments.UnNamed.Count = 1 Then
strShortcut = WScript.Arguments.UnNamed(0)
Else
WScript.Echo "Please supply the name of an lnk file or directory to read, eg c:\test.lnk or c:\shortcuts"
WScript.Quit(1)
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strShortCut) Then ' Was a directory specified?
Set objFolder = objFSO.getFolder(strShortcut) ' Get the folder
For Each objfile in objFolder.Files ' For each file in the top-level directory
If objfile.type = "Shortcut" Then ' Is this file a shortcut?
Call Readshortcut(objFile.Path, strProperties) ' yes, read the properties
dtmCreationDate = objFile.DateCreated
WScript.Echo dtmCreationDate & "," & strProperties ' output the results
End If
Next
ElseIf objFSO.FileExists(strShortCut) Then ' Was an individual file specified?
Call Readshortcut(strShortcut, strProperties) ' read the properties of the file
WScript.Echo strProperties ' output the results
Else ' file-not-found
WScript.Echo "Error: Could not read '" & strShortcut & "'"
WScript.Quit(2)
End If
Set objFSO = Nothing
Function Readshortcut(ByRef strShortcut, ByRef strProperties)
set objWshShell = WScript.CreateObject("WScript.Shell") ' Create the shell object
set objShellLink = objWshShell.CreateShortcut(strShortcut) ' Execute the createshortcut method, which also retrieves an existing shortcut
strProperties = strShortCut & "," & objShellLink.TargetPath & "," & objShellLink.WindowStyle & "," & objShellLink.Hotkey & "," & objShellLink.IconLocation & "," & objShellLink.Description & "," & objShellLink.Arguments & "," & objShellLink.FullName & "," & objShellLink.WorkingDirectory & """"
' This propertly can be set, but not read? - objShellLink.RelativePath
Set objShellLink = Nothing
Set objWshshell = Nothing
End Function
Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.
4 comments:
Hi,
Your script was very valuable to me. Thank you.
M.G.
Excelent help Thanks
This script is great for reading the properties of a shortcut, however, I can't seem to be able to retrieve the value into a string for comparison. ie: strTargetPath = objShellLink.TargetPath
I'm off base here I'm sure, but I need to retrieve the value in TargetPath for comparing to another string. If TargetPath = "x" then delete the shortcut ie: If objShellLink.TargetPath = "x" Then
Is this possible?
Hi,
Not sure if this would help me or not, I have lots of shortcuts to webpages in one folder, and i need a script that will read the shortcut target link to the webpage, and write that to a textfile.
would this be able to help me in this problem.
Thanks in advance.
Post a Comment