Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\$user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\CheckLauncher.lnk")
$Shortcut.TargetPath = "cmd.exe"
$Shortcut.Arguments = "C:\Users\Administrator\Desktop\Packages\checkLauncher.bat"

Error

Exception calling "Save" with "0" argument(s): "Unable to save shortcut "C:\Use
rs\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
CheckLauncher.lnk"."
At C:\Users\Administrator\Desktop\Packages\Setup.ps1:22 char:15
+ $Shortcut.Save <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

I assure you that this path - C:\Users\Administrator\Desktop\Packages\checkLauncher.bat does exist. Please do help to find out what is wrong with this.

Any help is awaited.. Thanks in advance

share|improve this question
    
I have the administrative permissions. I was earlier creating shortcut for a ps scrip.. It was working fine. Now it isnt – worried Sep 19 '13 at 16:16

I can successfully create a shortcut using this approach however, have you defined $user somewhere else because that isn't defined by default. Perhaps you meant to use $env:username? Alternatively you could use $home to get the c:\users\<username> part of the path. And even better is to use this .NET method:

[Environment]::GetFolderPath([environment+specialfolder]::ApplicationData)

I think you also want to specify the arguments like so:

$Shortcut.Arguments = "/c C:\Users\Administrator\Desktop\Packages\checkLauncher.bat"

The following works fine for me:

$roaming = [environment]::GetFolderPath([environment+specialfolder]::ApplicationData)
$path = "$roaming\Microsoft\Windows\Start Menu\Programs\Startup\UpdateBinDir.lnk"
$WshShell = New-Object -comObject WScript.Shell
$shortcut = $WshShell.CreateShortcut($path)
$shortcut.TargetPath = "cmd.exe"
$shortcut.Arguments = "/c C:\bin\updateBinDir.bat"
$shortcut.Save()
share|improve this answer
    
I have $user defined in the same script. $user=[Environment]::UserName I have tried the /c in arguments too.. Stil it gives the same error – worried Sep 19 '13 at 16:23
    
I don't think that is the issue because in the error the path has been resolved.. There is Administrator inplace of $user – worried Sep 19 '13 at 16:27
    
I tried your code with only file name changes needed. Yet its giving the same error – worried Sep 19 '13 at 16:34
1  
If you are not running this code under the Administrator account, I doubt you have access to the bat file in the Administrator user dir. Try moving the bat file to a location where you have access as you run this code and then try to create the shortcut with that path. – Keith Hill Sep 19 '13 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.