Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Batch/VBScript/Powershell hybrid script for creating shortcut to shell folders

As stated in Q-title, I am trying to create and automate script for creating shortcuts to Shell-Special folders which doesn’t have a valid path per path-string validation rules.

I have tried both Batch/VBScript hybrid and Powershell script for creating shortcut to Target path explorer shell:AppsFolder which when clicked will open that path exactly and not the basic ThisPC or FileExplorer path.

First the Batch/VBScript hybrid that failed:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /p "Esc_LinkName=Name: "
set /p "Esc_LinkTarget=Target: "
SET cSctVBS=CreateFolderShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
  echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
  echo sLinkFile = oWS.ExpandEnvironmentStrings^("%USERPROFILE%\Desktop\!Esc_LinkName!"^)
  echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
::  echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
  echo oLink.TargetPath = "!Esc_LinkTarget!"
  echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>>!LOG! 2>>&1
SETLOCAL DISABLEDELAYEDEXPANSION && ENDLOCAL
pause && goto :eof

which throws error: CreateFolderShortcut.vbs(4, 1) Microsoft VBScript runtime error: Invalid procedure call or argument on this line echo oLink.TargetPath = "!Esc_LinkTarget!" because it can’t take target-path explorer shell:AppsFolder.

And now the Powershell script that failed:

$shell = New-Object -ComObject WScript.Shell
$destination = "$env:USERPROFILE\Desktop"
$shortcutPath = Join-Path -Path $destination -ChildPath 'My Apps Folder.lnk'
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $shell.SpecialFolders.Item("AppsFolder")
$shortcut.Save()

which creates a shortcut to FileExplorer path instead.

Anyone can suggest, how to make this work to get what I want ?

>Solution :

It’s definitely worth sticking with a PowerShell script (.ps1), as it doesn’t require any trickery (though note that .ps1 files aren’t directly executable from outside a PowerShell session):

Replace:

$shortcut.TargetPath = $shell.SpecialFolders.Item("AppsFolder")

with:

$shortcut.TargetPath = 'shell:AppsFolder'

Complete code:

$shell = New-Object -ComObject WScript.Shell
$destination = "$env:USERPROFILE\Desktop"
$shortcutPath = Join-Path -Path $destination -ChildPath 'My Apps Folder.lnk'
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = 'shell:AppsFolder'
$shortcut.Save()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading