2

I'm trying to automate some stuff for users and one of them is to add "Computer" and "Documents" shortcut to their desktop.

I found the code below online and changed the target to "explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut(C:\users\username\Desktop\Computer.lnk")
$Shortcut.TargetPath = "explorer.exe \/e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$Shortcut.Save()

But when I run this code I get the following error :

"Exception calling "Save" with "0" argument : "Unable to save shotcut"

If there is another and easy way, I would love to hear it :)

Thank you everyone in advance.

3
  • 1
    This is simpler using group policy preferences, as documented here. Commented Sep 4, 2014 at 22:14
  • I get a completely different error when trying to set TargetPath with this ("TargetPath: The parameter is incorrect"). Try setting the TargetPath = "explorer.exe" and Arguments = "\/e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}". Also, is the shortcut in the current user's folder or a different user? Commented Sep 5, 2014 at 0:19
  • Hi Mike. I'm running the script with my admin account to create the shortcut on the users desktop. Matt's script below works like a charm except when I run the script with my admin account I get the error message. Commented Sep 5, 2014 at 14:07

1 Answer 1

1

I think i have a nice solution here. Also, in my own ineptitude, I think i figured out your error cause

First I found a cleaner way to make shortcuts to special folders.

$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\users\user\Desktop\MacadizamianNizzut.lnk")
$Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
$Shortcut.Save()

You could also use mydocuments in place of mycomputer. For a complete list of special folders that you can use: [enum]::GetNames([System.Environment+SpecialFolder]). Tips hat to JRV for a comment on my link above.

As for your error "Exception calling "Save" with "0" arguments : "Unable to save shortcut". I also got that error. In practice it was because the value passed for createshortcut was not a valid path. I am not saying that the file has to exist but the folder path does. I made a typo and got the error. Using my example this command would have failed: Test-Path ""C:\users\user\Desktop"

Some Error Prevention

What we could do is assign the shortcut path to a variable and test the path based on that.

$ShortcutPath = "C:\users\username\desktop\test.lnk"
If(Test-Path -Path (Split-Path -Path $ShortcutPath -Parent)){
    $WshShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut($ShortcutPath)
    $Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
    $Shortcut.Save()
} Else {
    Write-Host "Unable to create shortcut. Check the path $ShortcutPath."
}
Sign up to request clarification or add additional context in comments.

7 Comments

This works perfect. Thank you so much. The only issue is that I'm still getting the "Exception calling..." error if I run the script with my admin account (Which is how I will be running the script). However, if I run the script with the user account it works perfect. Any ideas on how to get around this?
What are you using for CreateShortcut("C:\users\user\Desktop\MacadizamianNizzut.lnk") in your script exactly. I would want to your if your equivelent passes this test: Test-Path "C:\users\user\Desktop"
I just tried the test-path and got access denied. Very weird. But this is my admin account. Why would I get access denied?
Are you running as administrator? I mean do you have UAC enabled? You still have to elevate the rights of your sessions.
I'm running PS with my admin account.
|

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.