Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Creating shortcuts from PowerShell

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-04-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]
Guest


 

Creating shortcuts from PowerShell

Below is a generic PowerShell wrapper script for generating standard Windows
shortcuts. Comments appreciated.


#New-Shortcut.ps1
Param
(
[string]$Source, #filesystem path to shortcut target
[string]$Target = $Source + ".lnk", #Target here is the new lnk file.
[string]$Arguments = $null,
[string]$Description = $null,
[string]$IconLocation = $null,
[string]$WorkingDirectory = $null,
[int]$WindowStyle = $null,
[string]$Hotkey = $null
)
$WshShell = New-Object -ComObject WScript.Shell
$link = $WshShell.CreateShortcut($Target);
$link.TargetPath = $Source;
if($Arguments){$link.Arguments = $Arguments;}
if($Description){$link.Description = $Description;}
if($IconLocation){$link.IconLocation = $IconLocation;}
if($WorkingDirectory){$link.WorkingDirectory = $WorkingDirectory;}
if($WindowStyle){$link.WindowStyle = $WindowStyle;}
if($Hotkey){$link.Hotkey = $Hotkey;}
$link.Save();



My System SpecsSystem Spec
Old 10-04-2006   #2 (permalink)
Sung M Kim
Guest


 

Re: Creating shortcuts from PowerShell


Alex K. Angelopoulos [MVP] wrote:
> Below is a generic PowerShell wrapper script for generating standard Windows
> shortcuts. Comments appreciated.
>
>
> #New-Shortcut.ps1
> Param
> (
> [string]$Source, #filesystem path to shortcut target
> [string]$Target = $Source + ".lnk", #Target here is the new lnk file.
> [string]$Arguments = $null,
> [string]$Description = $null,
> [string]$IconLocation = $null,
> [string]$WorkingDirectory = $null,
> [int]$WindowStyle = $null,
> [string]$Hotkey = $null
> )
> $WshShell = New-Object -ComObject WScript.Shell
> $link = $WshShell.CreateShortcut($Target);
> $link.TargetPath = $Source;
> if($Arguments){$link.Arguments = $Arguments;}
> if($Description){$link.Description = $Description;}
> if($IconLocation){$link.IconLocation = $IconLocation;}
> if($WorkingDirectory){$link.WorkingDirectory = $WorkingDirectory;}
> if($WindowStyle){$link.WindowStyle = $WindowStyle;}
> if($Hotkey){$link.Hotkey = $Hotkey;}
> $link.Save();


I have been wondering how to create a shortcut in PowerShell Thank
you for sharing the script.

Is there an option where to save the shortcut to? I am not sure what
"WorkingDirectory" refers to though.

Would you be able to give some usages on how to use the function?
thank you in advance

My System SpecsSystem Spec
Old 10-04-2006   #3 (permalink)
=?Utf-8?B?ZHJlZXNjaGtpbmQ=?=
Guest


 

Re: Creating shortcuts from PowerShell

"Sung M Kim" wrote:

> Is there an option where to save the shortcut to?


I think that is what the $target param is for:
[string]$Target = $Source + ".lnk", #Target here is the new lnk file.

> I am not sure what
> "WorkingDirectory" refers to though.


The current working directory of the app when it is started using this
shortcut.
In case of cmd.exe this is your current directory
(usually this is %HOMEDRIVE%%HOMEPATH% or the same as the executable path).
You can also set this directory in the file properties of a shortcut.

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 10-04-2006   #4 (permalink)
Alex K. Angelopoulos [MVP]
Guest


 

Re: Creating shortcuts from PowerShell

"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news:9B046CCB-692E-436F-AFD2-57D1AC97F1B1@microsoft.com...
> "Sung M Kim" wrote:
>
>> Is there an option where to save the shortcut to?


> I think that is what the $target param is for:
> [string]$Target = $Source + ".lnk", #Target here is the new lnk file.


Yep.

What it comes down to is that if you just want to automate shortcut
creation, it creates the shortcut in the same location as the original item,
with the same name and ".lnk" appended. So if you did
New-Shortcut C:\temp
you create a shortcut file C:\temp.lnk.

Obviously this is very minimalistic. Note that if you specify another
name/location, you have to explicitly include the ".lnk" suffix yourself.
That may be worth changing.

>> I am not sure what
>> "WorkingDirectory" refers to though.

>
> The current working directory of the app when it is started using this
> shortcut.
> In case of cmd.exe this is your current directory
> (usually this is %HOMEDRIVE%%HOMEPATH% or the same as the executable
> path).
> You can also set this directory in the file properties of a shortcut.


Slight correction. Windows normally sets the cmd.exe home directory to that,
but in the case of a shortcut, if you don't specify an explicit working
directory, you just inherit it from the launching process. In the case of a
clicked-on shortcut this is typically the folder where the shortcut resides.




My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help creating shortcuts Mike VB Script 3 08-04-2008 10:16 AM
Creating desktop shortcuts Arnold R Vista General 9 04-29-2008 01:04 AM
Creating shortcuts with PowerShell greatbarrier86 PowerShell 12 08-14-2007 08:34 AM
Vista Not Creating Shortcuts srm Vista General 4 05-04-2007 10:28 AM
creating Startmenu-Shortcuts Jens Schulze PowerShell 5 10-04-2006 10:00 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008