0

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:

@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 ?

1

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.